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.
Example2:- To view size of each file along with listing.
$ ls -l -a
Example3:- List along with sorting based on date and time file or directory.
$ ls -l -s
Example4:- List with access time of source file and sort based on access time.
$ ls -l -t
$ ls -u -l -t
- cd :- Changing the directory.
Example1:- Changing directory based on path specified.
Example2:- Changing directory to home.
$ cd /var/dir1
- pwd :- This command views present working directory
- mkdir :- This command allows the user to create single or multiple directories and set permission.
Example1:- Create new directory.
Example2:- Create multiple directory.
$ mkdir dir1
Example3:- Create nested directories.
$ mkdir dir2 dir3 dir4 dir5
Example4:- Print message for directory creation.
$ mkdir -p linux/redhat/centos
$ mkdir -v dir1
- rm :- Command used to remove file as well as non empty directory
Example1:- Removing file.
Example2:- Force remove file.
$ rm file.txt
Example3:- Remove directory along with files.
$ rm -f file.txt
Example4:- Force remove directory along with files.
$ rm -r dir1
$ rm -rf dir1
- cp :- To copy file/directories from one file to another.
Example1:- Copy file to dir1.
Example2:- Copy dir2 to dir1 with contents.
$ cp f1.txt dir1/
Example3:- Copy content of one file to another.
$ cp -r dir2/ dir1/
Example4:- Copy multiple directory with contents to one directory.
$ cp f1.txt f2.txt
$ 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.
Example2:- Create new file.
$ touch -m f1.txt
Example3:- Create multiple files together.
$ touch file.txt
$ 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.
Example2:- Move multiple files to directory.
$ mv file.txt dir1/
Example3:- Move one directory to another.
$mv file1.txt file2.txt file3.txt file4.txt file5.txt dir1/
Example4:- Move multiple directory to another directory.
$ mv dir1 box
Example5:- Rename a file.
$ mv dir2 dir3 dir4 box
Example6:- Rename a directory.
$ mv file.txt file1.txt
$ 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.
Example2:- Print multiple file contents.
$ tail /etc/auto.master
Example3:- Print last 3 lines of file.
$ tail /etc/auto.master /etc/auto.misc
Example4:- Viewing latest end of file contents.
$ tail -n 3 /etc/group
$ tail -f var/log/dnf.log