Linux For DevOps | File Commands

Linux For DevOps | File Commands


4 min read

In this blog we will cover most of the daily usage file commands which is required as BAU from devops perspective.

  1. ls :- It is used to list down the files and sub-directories within your current directory. Lets see few examples for the same.
    Example1:- To view all files along with hidden files.
    $ ls -l -a
    image.png Example2:- To view size of each file along with listing.
    $ ls -l -s
    image.png Example3:- List along with sorting based on date and time file or directory.
    $ ls -l -t
    image.png Example4:- List with access time of source file and sort based on access time.
    $ ls -u -l -t
  2. cd :- Changing the directory.
    Example1:- Changing directory based on path specified.
    $ cd /var/dir1
    image.png Example2:- Changing directory to home.
    $ cd
  3. pwd :- This command views present working directory
    $ pwd
  4. mkdir :- This command allows the user to create single or multiple directories and set permission.
    Example1:- Create new directory.
    $ mkdir dir1
    image.png Example2:- Create multiple directory.
    $ mkdir dir2 dir3 dir4 dir5
    image.png Example3:- Create nested directories.
    $ mkdir -p linux/redhat/centos
    image.png Example4:- Print message for directory creation.
    $ mkdir -v dir1
  5. rm :- Command used to remove file as well as non empty directory Example1:- Removing file.
    $ rm file.txt
    image.png Example2:- Force remove file.
    $ rm -f file.txt
    image.png Example3:- Remove directory along with files.
    $ rm -r dir1
    image.png Example4:- Force remove directory along with files.
    $ rm -rf dir1
  6. cp :- To copy file/directories from one file to another.
    Example1:- Copy file to dir1.
    $ cp f1.txt dir1/
    image.png Example2:- Copy dir2 to dir1 with contents.
    $ cp -r dir2/ dir1/
    image.png Example3:- Copy content of one file to another.
    $ cp f1.txt f2.txt
    image.png Example4:- Copy multiple directory with contents to one directory.
    $ cp -r dir2/ dir1/ dir3
  7. touch :- This command is used to change a files access and modifies/changes timestamp to current date and time. But if the file does not exist it will create a new file.
    Example1:- Change file timestamp.
    $ touch -m f1.txt
    image.png Example2:- Create new file.
    $ touch file.txt
    image.png Example3:- Create multiple files together.
    $ touch file1.txt file2.txt file3.txt file4.txt
  8. mv :- This command is used to move/rename files from one directory to another.
    Example1:- Move file to directory.
    $ mv file.txt dir1/
    image.png Example2:- Move multiple files to directory.
    $mv file1.txt file2.txt file3.txt file4.txt file5.txt dir1/
    image.png Example3:- Move one directory to another.
    $ mv dir1 box
    image.png Example4:- Move multiple directory to another directory.
    $ mv dir2 dir3 dir4 box
    image.png Example5:- Rename a file.
    $ mv file.txt file1.txt
    image.png Example6:- Rename a directory.
    $ mv box box1
  9. tar:- Its stands for tape archive and is used to create archive and extract the archive files. It is used to create compressed files or uncompress archived files and also modify them.
    Example1:- Create tar file.

    $ tar -cvf archive.tar dir/

    image.png Example2:- Create tar.gz file.

    $ tar -czvf archive.tar.gz dir/

    image.png Example3:- Create tar.bz2 file.

    $ tar -cjvf archive.tar.bz2 dir/

    Example4:- Create tar.xz file.

    $ tar -cJvf archive.tar.xz dir/

    image.png Example5:- Viewing tar file contents.

    $ tar -tvf archive.tar

    image.png Example6:- Extract tar file contents.

    $ tar -xvf archive.tar -C extract


  10. head :- Prints top N number of data given in input. By default it prints 10 lines of specified files. If more than 1 filename is provided then data from each file is preceded by its filename.
    Example1:- Print file contents.

    $ head /etc/passwd

    image.png Example2:-Print multiple file contents.

    $ head /etc/passwd /etc/group

    image.png Example3:- Print top 3 lines of file.

    $ head -n 3 /etc/passwd


  11. tail :- Prints data from end of the file.Usually data is added to the end of the file so tail command is quick and easy way to see the most recent addition.
    Example1:- Print file contents from end of file.
    $ tail  /etc/auto.master
    image.png Example2:- Print multiple file contents.
    $ tail /etc/auto.master /etc/auto.misc
    image.png Example3:- Print last 3 lines of file.
    $ tail -n 3 /etc/group
    image.png Example4:- Viewing latest end of file contents.
    $ tail -f var/log/dnf.log

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!