Basic Linux Commands

Basic Linux Commands

  1. ls

    ls is a Linux shell command that lists directory contents of files and directories. It provides valuable information about files, directories, and their attributes.

Ls Command Default

ls -a

To show all the hidden files in the directory, use ‘-a option’. Hidden files in Unix starts with ‘.’ in its filename . It will show all the files including the ‘.’ (current directory) and ‘..’ (parent directory).

ls -a

ls -l

linux ls -l command, display detailed information of non-hidden files and directories.

ls -l

  • ls -lt

To sort the file names displayed in the order of last modification time. You will find it handy to use it in combination with -l option.

ls -l

ls -S

Sort files and directories by their sizes, listing the largest ones first.

ls -lh

ls -lh (h stands for human readable form) to display file size in easy-to-read format. i.e M for MB, K for KB, G for GB.

ls -lh

ls -lrt

linux ls -lrt command, sort non-hidden files and directories by date from old to new.

  • ls -r reverse the order

  • ls -t sort by time modified

2.📝 touch - Create New Files With the "touch" command, you can create a new empty file in an instant! Simply type "touch filename.extension" and, ta-da! 🪄 Your new file is ready to be filled with whatever you desire.

  • To change the access permissions of files.

if you run ls -l, it will show the permissions, owner, size, and last modified date for each file in the directory.

Chmod: This command is used to change the access permissions of files and directories.

For example: Following “chmod” command will give the user permission to read, write and execute a file.

  • To create a fruits.txt file and to view the content.

There are 3 ways or Commands to create and view the content

For Creation For Viewing

touch fruits.txt cat fruits.txt

nano fruits.txt nano fruits.txt

vim fruits.txt vim fruits.txt

  • Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

for adding content in Devops.txt i used nano file editor

for example nano Devops.txt this command will open the file then simply add the content like : Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava

after adding content press ctrl+x then Y for yes it will save the file and then hit Enter.

  • To Show only top three fruits from the file.

Command ---> head -3 filename

for example head -3 Devops.txt

  • To Show only bottom three fruits from the file.

Command ----> tail -3 filename

for example tail -3 Devops.txt

To check which commands you have run till now.

you just need to use a command i.e history

To find the difference between two files.

The diff command compares the contents of two files and reports the differences between them. Here's how to use it:

diff fruits.txt Colors.txt

To remove a directory/ Folder.

A. You can use the rmdir or rm command. The rmdir command is used specifically for removing empty directories, while the rm command can be used to remove both empty and non-empty directories. Here's how to use both commands:

Using rmdir (for empty directories): rmdir directory_name

Using rm (for empty and non-empty directories)

To remove an empty directory: rm -d directory_name

To remove a non-empty directory and its contents (use with caution):

rm -r directory_name

To forcefully and recursively remove a directory and its contents without prompting (use with extreme caution): rm -rf directory_name