In this blog we will cover most of the daily usage file commands which is required as BAU from devops perspective.
- 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
Example2:- To view size of each file along with listing.
$ ls -l -s
Example3:- List along with sorting based on date and time file or directory.
$ ls -l -t
Example4:- List with access time of source file and sort based on access time.
$ ls -u -l -t
- cd :- Changing the directory.
Example1:- Changing directory based on path specified.$ cd /var/dir1
Example2:- Changing directory to home.
$ cd
- pwd :- This command views present working directory
$ pwd
- mkdir :- This command allows the user to create single or multiple directories and set permission.
Example1:- Create new directory.$ mkdir dir1
Example2:- Create multiple directory.
$ mkdir dir2 dir3 dir4 dir5
Example3:- Create nested directories.
$ mkdir -p linux/redhat/centos
Example4:- Print message for directory creation.
$ mkdir -v dir1
- rm :- Command used to remove file as well as non empty directory
Example1:- Removing file.
$ rm file.txt
Example2:- Force remove file.
$ rm -f file.txt
Example3:- Remove directory along with files.
$ rm -r dir1
Example4:- Force remove directory along with files.
$ rm -rf dir1
- cp :- To copy file/directories from one file to another.
Example1:- Copy file to dir1.$ cp f1.txt dir1/
Example2:- Copy dir2 to dir1 with contents.
$ cp -r dir2/ dir1/
Example3:- Copy content of one file to another.
$ cp f1.txt f2.txt
Example4:- Copy multiple directory with contents to one directory.
$ cp -r dir2/ dir1/ dir3
- 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
Example2:- Create new file.
$ touch file.txt
Example3:- Create multiple files together.
$ touch file1.txt file2.txt file3.txt file4.txt
- mv :- This command is used to move/rename files from one directory to another.
Example1:- Move file to directory.$ mv file.txt dir1/
Example2:- Move multiple files to directory.
$mv file1.txt file2.txt file3.txt file4.txt file5.txt dir1/
Example3:- Move one directory to another.
$ mv dir1 box
Example4:- Move multiple directory to another directory.
$ mv dir2 dir3 dir4 box
Example5:- Rename a file.
$ mv file.txt file1.txt
Example6:- Rename a directory.
$ mv box box1
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/
Example2:- Create tar.gz file.
$ tar -czvf archive.tar.gz dir/
Example3:- Create tar.bz2 file.
$ tar -cjvf archive.tar.bz2 dir/
Example4:- Create tar.xz file.
$ tar -cJvf archive.tar.xz dir/
Example5:- Viewing tar file contents.
$ tar -tvf archive.tar
Example6:- Extract tar file contents.
$ tar -xvf archive.tar -C extract
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
Example2:-Print multiple file contents.
$ head /etc/passwd /etc/group
Example3:- Print top 3 lines of file.
$ head -n 3 /etc/passwd
- 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
Example2:- Print multiple file contents.
$ tail /etc/auto.master /etc/auto.misc
Example3:- Print last 3 lines of file.
$ tail -n 3 /etc/group
Example4:- Viewing latest end of file contents.
$ tail -f var/log/dnf.log