Day 2 Task: Basics Linux command

Day 2 Task: Basics Linux command

Listing commands

Here are some examples of different ls commands in Linux with explanations:

  • ls - This command lists the files and directories in the current working directory:

      $ ls
      Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
    
  • ls -a - This command lists all files and directories, including hidden files:

      $ ls -a
      .  ..  .bash_history  .bashrc  .config  Desktop  Documents  Downloads  .local  Music  Pictures  .ssh  .sudo_as_admin_successful  .viminfo  Public  Templates  Videos
    
  • ls -l - This command lists files and directories in long format, which includes details like permissions, ownership, size, and modification date/time:

      $ ls -l
      total 16
      -rw-r--r-- 1 user user   11 Feb 23 14:02 file1.txt
      -rw-r--r-- 1 user user   22 Feb 23 14:02 file2.txt
      -rw-r--r-- 1 user user   23 Feb 23 14:02 file3.txt
      drwxr-xr-x 2 user user 4096 Feb 23 14:02 dir1
      drwxr-xr-x 2 user user 4096 Feb 23 14:02 dir2
    

    The output is displayed in multiple columns, with each column separated by whitespace. The first column shows the file type and permissions, followed by the number of hard links, the owner name, the group name, the size of the file in bytes, the modification time, and finally the file name.

    For example, the first line shows -rw-r--r-- in the first column, which indicates that the file file1.txt has read and write permissions for the owner, and read-only permissions for the group and others. The number 1 in the second column indicates that there is one hard link to this file. The owner and group names are both user. The size of the file is 11 bytes. The modification time is Feb 23 14:02, and the file name is file1.txt.

  • ls -lh - This command lists files and directories in long format with human-readable file sizes:

      $ ls -lh
      total 68K
      drwxr-xr-x 2 user user 4.0K Feb 10 15:10 Desktop
      drwxr-xr-x 3 user user 4.0K Feb 10 15:08 Documents
      drwxr-xr-x 2 user user 4.0K Feb 10 15:10 Downloads
      drwxr-xr-x 2 user user 4.0K Feb 10 15:10 Music
      drwxr-xr-x 2 user user 4.0K Feb 10 15:10 Pictures
      drwxr-xr-x 2 user user 4.0K Feb 10 15:10 Public
      drwxr-xr-x 2 user user 4.0K Feb 10 15:10 Templates
      drwxr-xr-x 2 user user 4.0K Feb 10 15:10 Videos
    
  • ls -t - This command lists files and directories sorted by modification time, with the newest files first:

      $ ls -t
      Downloads  Documents  Pictures  Public  Videos  Music  Templates  Desktop
    
  • ls -r - This command lists files and directories in reverse order:

      $ ls -r
      Videos  Templates  Public  Pictures  Music  Downloads  Documents  Desktop
    
  • ls -R - This command lists files and directories recursively, i.e., it also lists the contents of subdirectories:

      $ ls -R
      .:
      Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
    
      ./Desktop:
      file1.txt  file2.txt
    
      ./Documents:
      file3.txt  file4.txt
    
      ./Downloads:
      file5.txt  file6.txt
    
      ./Music:
      file
    
  • ls -i: Lists files and directories with their inode numbers.

      $ ls -i
       3957718 file1.txt
       3957720 file2.txt
       3957722 file3.txt
       3957719 dir1
       3957721 dir2
    
  • ls -S: Lists files and directories sorted by size, with the largest files first.

      $ ls -S
      total 162M
      -rw-r--r-- 1 user user 120M Feb 15 10:45 large_file.iso
      -rw-r--r-- 1 user user  20M Feb 15 10:47 medium_file.mp4
      -rw-r--r-- 1 user user   2M Feb 15 10:48 small_file.txt
      drwxr-xr-x 2 user user 4.0K Feb 15 10:49 scripts
      drwxr-xr-x 3 user user 4.0K Feb 15 10:50 documents
    
  • ls *.sh: list all the files having .sh extension.

      $ ls *.sh
      script1.sh  script2.sh  script3.sh
    

Directory commands

Some commonly used Linux directory commands with examples:

Current Working

pwd: This command displays the current working directory.

$ pwd
/home/user/Documents

Change Directory Commands:

  • cd [directory]: Change to a specific directory

      $ cd /usr/local/bin
    
  • cd ~: Change to the home directory

      $ cd ~
    
  • cd -: Change to the previous directory

      $ cd -
    
  • cd ..: Change to the parent directory

      $ cd ..
    
  • cd [relative_directory_path]: Change to the directory relative to the current directory

      $ cd ../my_directory
    

Make Directory Commands:

  • mkdir [directory]: Create a single directory

      [root@localhost demo]# mkdir my_directory
      [root@localhost demo]# ls
      my_directory
    
  • mkdir [directory1] [directory2] [directory3] ...: Create multiple directories

      [root@localhost demo]# mkdir D1 D2 D3
      [root@localhost demo]# ls
      D1            D2            D3
    
  • mkdir -m [permission] [directory]: Create a directory with a specific permission

      $ mkdir -m 755 my_directory
    
  • mkdir -p [parent_directory]/[child_directory]: Create a directory with multiple parent directories

      mkdir -p my_parent_directory/my_child_directory
    
  • mkdir -m [permission] -o [owner] -g [group] [directory]: Create a directory with a specific owner and group

      $ mkdir -m 755 -o user -g group my_directory
    

    This will create a directory named my_directory with the permission 755, owned by the user user and the group group.

Move and Rename Commands:

  • mv [old_name] [new_name]: Rename a file or directory

  •     mv file1.txt file2.txt
    
  • mv [source] [destination]: Move a file or directory to a new location

  •     $ mv file1.txt /home/user/Documents/
    
  • mv [old_name] [new_name] [destination]: Move a file or directory and rename it

  •     $ mv file1.txt file2.txt /home/user/Documents/
    

    Thank you for reading! Hope you find this article helpful.

    ~Kunal