Day-3: Basic Commands in Linux

Day-3: Basic Commands in Linux

1- ls Command

The ls command in Linux is used to list directory contents. It provides an overview of the files, directories, and their details within the current or specified directory.

Now I will explain the options which we can use with ls command along with their description.

ls -l

The option “ -l “ is long format which will display the information about files and directories.

ls -a

The option “ -a “ will show all files and directories including those which are hidden.

ls -t

The option “ -t “ will sort the files and directories in a way in which they are modified. This will show the file on top which is recently modified.

ls -r

The option “ -r “ will display the files and directories in the reverse order.

ls -s

ls -lsh

The option “ -s “ is use to sort the files and directories according to their sizes. We can use “ -h “ option with this which will show it in human readable form.

ls -R

the option “ -R “ is used to list files and directories recursively including subdirectories

ls -i

The option “ -i “ known as inode, this will show index number for each file and directory.

even hidden files and directories have “inode“ numbers.

ls -g

The option “ -g “ will show the group ownership of files and directories instead of owner.

ls -h

The Option “ -h “ is use to display in human readable format.

ls -d

The -d option with the ls command in Linux lists information about directories themselves, rather than their contents.

ls -F

The ls -F command in Linux adds a special character at the end of each listed item to indicate its type. This makes it easier to distinguish between files, directories, and other types of items in the output (e.g., / for directories, * for executables).

ls -n

The option “n” will display information in numerical format including user and group ID instead of their names.

File Types

  • *: Normal file

  • d: Directory

  • s: Socket file

  • l: Link file

2- cd Command

cd <directory name>

The “cd <directory Name>” command in Linux is used to change the current working directory. It allows you to navigate between directories in the file system.

cd ..

This command will take you one step back to parent directory.

cd /path/to/dir

This command will navigate to absolute path

cd -

This command will return to the previous directory where you were in.

cd ../..

This will take you two levels up in directory hierarchy.

cd /

This will navigate to root directory of the file system

cd /path/to/dir && pwd

This will combine the commands in which you are changing the directories and confirming the present working directory.

3- pwd Command

The “pwd” command is used to display the full path of user’s current working directory in the Linux file system.

4- cp Command

The “cp” command in Linux is used to copy the files and directories from one location to another.

cp -r

The “-r” option is use to copy the directories recursively from one place to another.

cp -i

The option “-i” will give prompt before copying

cp -v

The option “v” known as verbose will show output during operation

5- mv Command

The mv command is use to move or rename the files and directories in Linux File System.

mv -i

The option “-i” in this will display prompt before moving or renaming the file or directory. It will only ask prompt if the file or directory with same name already exist.

mv -v

This will display verbose output during the operation.

6- rm Command

The rm command in Linux is using to remove the file or directory.

rm -r

rm -i

This will remove the directory and it’s subdirectories recursively and -i will prompt before removing the file and directory.

rm -rf

This will remove the file, directory and it’s subdirectories by force and recursively.

7- mkdir Command

This command is use to make directory in Linux.

mkdir -p

This command will create parent directories as needed.

mkdir -v

This command will show the verbose output

8- Touch Command

The touch command in Linux is used to create empty files or update the timestamps of existing files (access and modification time).

touch -c

This command do not create file if they don’t exist.

touch -t

This specify the timestamp of the file.

9- cat Command

We use cat command to concatenate and display the content of the file.

cat -n

This will number all the output lines (including white spaces).

cat -b

This will number all the output lines (excluding white spaces).

10- less Command

With help of less command we can view file content one screen at a time.

Examples

  1. View a file:

    less file.txt

  2. Search for a keyword in a file:

    less file.txt

    /keyword

  3. Show line numbers while viewing:

    less -N file.txt

11- head Command

This command display’s the content from beginning of file.

Examples

  1. Display the first 10 lines of a file:

    head file.txt

  2. Display the first 5 lines of a file:

    head -n 5 file.txt

12- tail Command

This command is use to display the content from end of the file.

Examples

  1. Display the last 10 lines of a file:

    tail file.txt

  2. Display the last 5 lines of a file:

    tail -n 5 file.txt

  3. Follow a file in real-time:

    tail -f file.txt

I hope you enjoy reading the Blog and if you like, do reshare with the community !!