File and directory commands

 

File and directory commands


  1. pwd The pwd(Present Working Directory) command is used to print the name of the present/current working directory starting from the root.

    $ pwd
    /home/sj/Desktop/Linux
  2. ls: The ls command is used to list files or directories. It also accepts some flags or options that changes how files or directories are listed in your terminal.

     Syntax:
     ls [flags] [directory]
    
     Example:
     $ ls
     bin dev lib libx32 mnt  
    
     //Listing files & directories with time in a rever order
     $ ls -ltr
     drwxr-xr-x 2 sj sj 4096 May 14  2020 Videos
     drwxr-xr-x 2 sj sj 4096 May 14  2020 Templates
     drwxr-xr-x 2 sj sj 4096 May 14  2020 Public
    
     //Home directory
     $ ls ~
     Desktop    Downloads  Pictures  Sudheer    test   test.txt
     Documents  Music      Public    Templates  test1  Videos

    Below are the list of possible options for ls command,

    -a Show all (including hidden)
    -R Recursive list
    -r Reverse order
    -t Sort by last modified
    -S Sort by file size
    -l Long listing format
    -1 One file per line
    -m Comma-­sep­arated output
    -Q Quoted output
  3. mkdir The mkdir(make directory) command allows users to create directories or folders.

    $ mkdir ubuntu
    $ ls
    ubuntu

    The option '-p' is used to create multiple directories or parent directories at once.

    $ mkdir -p dir1/dir2/dir3
    $ cd dir1/dir2/dir3
    ~/Desktop/Linux/dir1/dir2/dir3$
  4. rmdir: The rmdir(remove directories) is used to remove empty directories. Can be used to delete multiple empty directories as well. Safer to use compared to rm -r FolderName. This command can also be forced to delete non-empty directories.

    1. Remove empty directory:
    rmdir FolderName
    1. Remove multiple directories:
    rmdir FolderName1 FolderName2 FolderName3
    1. Remove non-empty directories:
    rmdir FolderName1 --ignore-fail-on-non-empty
    1. Remove entire directory tree. This command is similar to rmdir a/b/c a/b a:
    rmdir -p a/b/c
  5. rm: The rm(remove) command is used to remove objects such as files, directories, symbolic links etc from the file system.

    1. Remove file: The rm command is used to remove or delete a file
    rm file_name
    1. Remove file forcefully: The rm command with -f option is used for removal of file without prompting for confirmation.
    rm -f filename
    1. Remove directory: The rm command with -r option is used to remove the directory and its contents recursively.
    rm -r myDir
    1. Remove directory forcefully: The rm command with -rf option is used to forcefully remove directory recursively.
    rm -rf myDir
  6. touch: The touch command is is used to create, change and modify timestamps of a file without any content.

    1. Create a new file: You can create a single file at a time using touch command. The file created is an empty file.

      touch file_name
    2. Create multiple files: You can create the multiple numbers of files at the same time.

      touch file1_name file2_name file3_name
    3. Change access time: The touch command with a option is used to change the access time of a file.

      touch -a file_name
    4. Change modification time: The touch command with m option is used to change the modified time.

      touch -m file_name
    5. Use timestamp of other file: The touch command with r option is used to get timestamp of another file.

      touch -r file2 file1

      In the above example, we get the timestamp of file1 for file2.

    6. Create file with Specific time: The touch command with 't' option is used to create a file with specified time.

      touch -t 1911010000 file_name
  7. cat: The cat command is used to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.

    $ cat [OPTION] [FILE]...
    1. Create a file: Used to create a file with specific name, content and press exit using CTRL + D

      cat > file_name1.txt
      Hello, How are you?
    2. View file contents: You can view contents of a single or more files by mentioning the filenames.

      cat file_name1 file_name2
    3. More & Less options: If a file having a large number of content that won’t fit in the output terminal then more & less options can be used to indiate additional content.

      cat file_name1.txt | more
      cat file_name1.txt | less

Comments