pwd – present working directory
To know where you are in the file system, type pwd. This will display current
folder path.
___________________________________________________________________________________
_____________________________________________
cd – Change directory
You can move to a directory using cd command.
___________________________________________________________________________________
_____________________________________________
man command:
displays online manual help.
Syntax: man commandname
Example: man ls , man cp, man chmod etc
___________________________________________________________________________________
_____________________________________________
ls command:
List the files and subdirectories in a directory.
The ls command does NOT list any dot files (i.e., files that begin with dot (.)
Type ls -a to list hidden files ( hidden files begin with ‘.’ Ex: “.credentials”
Here credentials is a hidden file as the file name starts with ‘.’)
ls -R to lists the files in the current directory as well as those in the
subdirectories.
ls -l for long listing
If you add a folder name or path, it will print that folder contents:
ls /bin
___________________________________________________________________________________
______________________________________________
mkdir command:
You create folders using the mkdir command:
Ex: mkdir project
You can create multiple folders with one command:
Ex: mkdir develop prod
You can also create multiple nested folders by adding the -p option:
EX: mkdir -p unix/scripts
rmdir command:
Just as you can create a folder using mkdir , you can delete a folder using rmdir.
You can also delete multiple folders at once.
The folder you delete must be empty.
To delete folders with files in them, we'll use the more generic rm command which
deletes files and folders, using the -r option.
Be careful as this command does not ask for confirmation and it will immediately
remove anything you ask it to remove.
Ex: rmdir project
rmdir develop prod
rmdir unix (this command must throw an error as unix directory has scripts
within it
and it is not empty)
rm -r unix
___________________________________________________________________________________
_______________________________________
cat command:
cat prints a file's content to the standard output. It can also add content to a
file, and this makes it super powerful.
Ex: create a file using cat command
cat>emp.txt
[Enter the input]
..
..
..
[ctrl+d to exit]
Ex: prints a file's content to the standard output:
cat emp.txt
You can print the content of multiple files.
Ex: cat file1 file2
Using the output redirection operator > you can concatenate the content of multiple
files into a new file.
Ex: cat file1 file2 > file3
Using >> you can append the content of multiple files into a new file, creating it
if it does not exist
Ex: cat file1 file2 >> file
When watching source code files it's great to see the line numbers, and you can
have cat print them using the -n option:
Ex: cat -n file1
cp command:
cp command is used to copy files.
Ex: cp emp.txt empbkup.txt
To copy folders you need to add the -r option to recursively copy the whole folder
contents.
Ex: cp -r projects projects_copy
In above example all the files and directories from projects will be copied to
projects_coy
Below is syntax of cp command. Here path can be absolute or relative.
cp <path>/srcfilename <path>/targetfilename
mv command:
mv command is used to move files or directories. You specify the file current path,
and its new path.
The difference between mv and cp is that cp places a copy of the file in a new
location without disturbing the original copy. The mv commands deletes the file
from its old location after saving it in the new location.
Ex: mv file project/
Moves file inside project directory (cut and paste)
Ex: mv sample sampledemo
Renames sample file to sampledemo (renaming file)
If the last parameter is a folder, the file located at the first parameter path is
going to be moved into that folder. In this case, you can specify a list of files
and they will all be moved in the folder path identified by the last parameter.
Ex: mv file1 file2 projects
rm command:
rm command is used to remove a file.
Ex: rm file
To invoke a prompt before removing a file, use -I option. waits for a "Y/y" or
"N/n" response.
Ex: rm -i sample2
find command:
The find command can be used to find files or folders matching a particular search
pattern. It searches recursively.
Ex: find . -name *.sh
Above example finds all files ending with .sh in the current directory.
cmp command:
Used to compare files.
Find below example for cmp command:
ln command:
Creates symbolic link between files.
Ex: ln -s emp emplinked
head command:
Displays first n lines of a file. Default is 10 lines if n value is not specified.
Ex: head -n filename
tail command:
Displays last n lines of a file. Default is 10 lines if n value is not specified.
Ex: tail -n filename
chmod command:
Used to change file permissions.
chmod can be used in 2 ways. The first is using symbolic arguments, the second is
using numeric arguments. Let's start with symbols first, which is more intuitive.
a stands for all
u stands for user
g stands for group
stands for others
Then you type either + or - to add a permission, or to remove it. Then you enter
one or more permissions symbols ( r , w , x ).
In numeric arguments, you use a digit that represents the permissions of the
persona. This number value can be a maximum of 7, and it's calculated in this way
1 if has execution permission
2 if has write permission
4 if has read permission
17)grep command:
used to find pattern in a file.
Below are some examples of using grep command with different options.
-i 🡪 ignore case
-v 🡪 inverse
-w 🡪 search for entire word
18)sort command:
used to sort file contents.
-n option to sort numeric data
-r to reverse the order
-u to include only unique
19) cut command:
You can cut files by specifying delimiter for delimited file or characters for
fixed length file.
cut command for delimited file: cut -d “,” -f2,4, emp2.txt
cut command for fixed length file: cut -c1-4,15-20 emp3.txt
20)paste command:
paste data vertically.
pipe(|)
you can combine multiple unix commands using pipe operator. Below are few examples:
Display name of person who has highest salary.
21)ps command:
list all processes using ps command.