Important Linux commands for Devops
What you will Learn:
pwd
Change directory commands: cd, cd ~, cd -
List commands: ls, ls -l, ls -la, ls –lh, ls -lht, ls –lhtr
touch command
cat command
tail command
head command
mkdir command
create multiple directories using -p switch
man command
cp command
rm command
Rename a file (mv command)
pwd
‘pwd’ command is used to print current working directory. This command tells us where we are in the file system at any point. When you execute ‘pwd’ command for the first time, it prints default home directory
cd commands
‘cd’ is to change the directory
Now if you execute ‘pwd’, the output shows / as expected
Similarly
Next, cd ~ (cd tilde) switches you back to default home directory, irrespective of where you are in the current file system
Next, let us change directory to /home and then to /etc.
When you now execute cd - it takes you back to previous directory (viz /home in current case)
ls commands
‘ls’ commands are used to list the contents of current directory. The contents can be files or directories
‘ls –l’ is the long listing
Let us cd to /etc and execute the same command, you can see the details of files and directories
Now, ‘ls -l’ command does NOT show the hidden files and directories. To see the hidden contents, we use the ‘a’ switch viz ‘ls -la’. As you can now see, we can see the hidden files (files starting with dot are hidden files)
Next, if you want to know the file size in human readable format, you can use ‘h’ switch viz ‘ls -lh’. So we can see below the file sizes (4K meaning 4 kilobyte, 3 kilobyte and so on)
Next, ‘ls -lht’ sorts the files based on timestamps (latest file first)
Next, ls -lhtr’ shows oldest file first
The above 2 commands can be used to find latest/oldest log file.
touch command
‘touch command can be used to create a new file. Let us cd to home directory and execute ‘touch testfile.txt’ command. You can see that a blank file has been created
cat command
‘cat’ command can be used to view the contents of a file
Let us cd to /var/log and execute ‘cat syslog’.
As you can see below, we can see the file contents
tail command
If the file contents are huge, you might want to see only the last few lines. This is generally true when you want to see the latest log file contents. So in order to see the only the last few lines of a log file, you can use the ‘tail’ command
head command
Similarly you can see the top few lines of a file using ‘head’ command
mkdir command
We can create a directory using mkdir command. Let us cd to home dir
create multiple directories using -p switch
Using -p switch you can create multiple directories hierarchy using a single command. As you can see below, d2 got created under d1 and d3 under d2
man command
You can use ‘man’ command to read the manual of any command. For example, ‘man ls’ will give you the complete manual of ‘ls’ command
You can scroll the manual by arrow down key
cp command
You can create a copy of a file using ‘cp’
Similarly, you can create a copy of directory using -r switch
rm command
‘rm’ command can be used to delete a file
Let us look at the manual of ‘rm’ command to find out how to delete a directory. After executing ‘man rm’, just type /directory at the bottom and hit enter. This will search all the keywords in the manual that match ‘directory’. See below, it tells us to use -r to remove a directory
This way, you don’t have to remove all the switches, simply use the ‘man’ command. Notice below that we have deleted the testdir2 directory
Rename a file (mv command)
To rename a file, simply use ‘mv’ command. Below we have renamed testfile2.txt to testfile4.txt
So these were very basic linux commands.
Thank you for reading!