Linux Commands You Should Know…..
This blog covers basic linux commands which are asked in interviews & good to have in your repo. Linux distribution supports various GUIs (graphical user interfaces) but the old command line interface (bash) still proves to be easier and quicker in some situations. Tasks that require a multi-step process through GUI can be done in a matter of seconds by typing commands into the CLI.
Here is a list of basic commands :
- pwd command : Print Working Directory
Use pwd command to find the path of your current working directory. The command will return an absolute (full) path, which is basically a path of all the directories that starts with a forward slash (/).
2. cd command : Change Directory
To navigate through the directories cd command is used. Let’s say if your current path is /Users/pavanpathak and you want to navigate to /Users all you have to do is run cd .. it will take you one directory back that’s /Users & if you run cd it will take you to home directory.
3. ls command : list files
ls command is used to list the files inside directory. By default it will show the files of current directory. But incase if you want to list the files of any other directory just run < ls pathname >.
ls -l will list the files & directories with full information like permission, ownership, size etc etc.
ls -al will list the hidden files as well.
ls -R will all the files inside subdirectories as well.
4. cp command : Copy
cp command is used to copy files from current directory to different directory. In below screenshot we have copied file pavan from /Users/pavanpathak/ to /Users/pavanpatha/Music.
5. mv command : Move
mv command basically works as cut in windows but here in linux apart from moving file from one directory to another it’s used for renaming file as well. In below screenshot you can see I have moved file pavan from /Users/pavanpatha/Music to /Users/pavanpatha/Music/myfile and after that I have renamed the same file inside /myfile directory from pavan to pavanpathak using mv command.
6. cat command : cat (short for “concatenate“)
cat (short for “concatenate“) command is one of the most frequently used commands in Linux/Unix operating systems. cat command allows us to create single or multiple files, view content of a file, concatenate files and redirect output in terminal or files.
- cat filename it will show the content of file.
- cat > filename creates a new file.
- cat filename1 filename2>filename3 joins two files (1 and 2) and stores the output of them in a new file (3)
- to convert a file to upper or lower case use, cat filename | tr a-z A-Z >output.txt
7. touch command :
The touch command is a standard command used in UNIX/Linux operating system which is used to create, change and modify timestamps of a file. Touch command creates empty files inside directory. You can create one or multiple files.
Touch command has various options. These options are very useful for various purpose.
touch -a: This command is used to change access time only. To change or update the last access or modification times of a file touch -a command is used. < touch -a filename >
touch -c : This command is used to check whether a file is created or not. If not created then don’t create it. This command avoids creating files. < touch -c filename >
touch -c-d : This is used to update access and modification time. < touch -c-d filename >
touch -m : This is used to change the modification time only. It only updates last modification time. < touch -m filename >
touch -r : This command is used to use the timestamp of another file. < touch -r secondfile firstfile >
touch -t : This is used to create a file using a specified time. < touch -t filename >
8. mkdir command :
mkdir command is used to make a new directory. If you type mkdir pavan-new it will make a new directory.
If you run mkdir pavan-new/pavan-2 it will make pavan2 directory inside pavan-new.
If you use p ( parent ) option it will create a new directory between two existing directories. mkdir -p pavan-new/pavan1/pavan2.
9. rmdir & rm command :
If you need to delete a directory, use rmdir command. rmdir allows you to delete empty directories.
< rmdir directoryname >
If you want to delete a directory with content in it use < rm -r directoryname > and if you want to force delete it use < rm -rf directoryname >
Note: Be very careful with rm command and double-check which directory you are in. This will delete everything and there is no undo.
10. locate & find command :
This command is just like search command in windows. If you don’t remember filename just use ( -i ) option. It will search the file with that name. (-i) will make it case insensitive. To search for a file that contains two or more words, use an asterisk (*). < locate -i pavan*pavan2 >
Similar to the locate command, using find also searches for files and directories. The difference is, you use the find command to locate files within a given directory.
As an example, find /home/ -name pavan.txt command will search for a file called notes.txt within the home directory and its subdirectories.
Other variations when using the find are:
- To find files in the current directory use, find . -name pavan.txt
- To look for directories use, / -type d -name pavan.txt
find will search in all directories and list all files.
11. head & tail command :
head command is used to view first lines of any text file. While tail command is used to view last lines of any text file. < head -n filename > < tail -n filename >
12. diff command :
The diff command compares the contents of two files line by line. After analyzing the files, it will output the lines that do not match.
Knowledge of basic command helps you to work effectively and efficiently. I will be adding more in this in coming days.
Feel free to comment if you are looking anything specific. I will try to add in coming days.