Linux commands¶
Commonly used commands in linux. Open up terminal using ctrl+alt+t
and do the exercises as you learn each command. Read Files before starting this since you will need it to do the exercises.
pipe¶
Pipe is used to redirect the output of one command/program to input of another command/program.
The character |
is used for this purpose. You can see the below example.
cat file | grep
gives the contents of file
to grep.
ls¶
Lists the contents of the current directory or the directory that we mentioned.
Usage¶
-
ls
- lists the contents of the current directory. -
ls dir
- lists the contents of directorydir
. -
ls -a dir
lists the contents including the hidden files/folders.- Hidden files/folders - files that start with a
.
- Hidden files/folders - files that start with a
-
ls -l file
- gives information about owner, creation date and time, permissions forfile
.
For more information,
$ man ls
- List the contents of
Desktop
directory.
mkdir¶
Used to create a new directory.
mkdir dir
- will make a new directorydir
.mkdir -p dir1/dir2
- creates the required parent directories if it dosent't already exist.
For more information,
$ man mkdir
- Create a directory with the name
myfirst
and a directorymysecond
insidemyfirst
. - Try to do this in one command.
cd¶
Used to change from one directory to another.
cd dir
- changes the directory todir
.cd
orcd ~
- moves to home directory.~
- /home/{user}/
cd ..
- moves to parent directory of current directory.
For more information,
$ man cd
- Change the working directory to
myfirst
.
touch¶
Used to create a empty file.
touch file
- creates a new empty file with filenamefile
.
For more information,
$ man touch
- Create a file
test
in the current directory.
cat¶
Display the contents of a file or concatenate files.
cat file
- displays contents offile
.cat file1 file2 > file3
- create a new filefile3
with contents of bothfile1
andfile2
cat file1 >> file2
- adds contents offile1
to end offile2
For more information,
$ man cat
- Print the contents of
test
.
cp¶
Used to make copies of files or directories.
cp file1 file2
- copies contents offile1
to a new filefile2
.cp -r dir1 dir2
- copies the directorydir1
to a new directorydir2
with the contents ofdir1
.
For more information,
$ man cp
- Copy
test
tomysecond
.
mv¶
Move a file or directory into another file or directory or rename files or directories.
mv file1 file2
- changes the name of file fromfile1
tofile2
.mv dir1 dir2
- changes the name of directory fromdir1
todir2
.mv file1 file2 dir1
- moves bothfile1
andfile2
to the directorydir1
given that the directory exists.
For more information,
$ man mv
- Move
mysecond
to~
.
rm & rmdir¶
Removes a file or a directory.
rm file1
- removes the filefile1
.rmdir dir1
- removes the directorydir1
only if the directory is empty.rm -rf dir1
- removes the directorydir1
even if it has contents.
For more information,
$ man rm
$ man rmdir
- Try removing
myfirst
directory usingrmdir
. What happened? - Now remove
test
insidemyfirst
usingrm
and try removingmyfirst
again. - Remove
mysecond
usingrm
. What happened?
echo¶
Gives the argument to standard output.
echo argument
- writes argument to standard output.
$ echo "Hello World!"
Hello World!
For more information,
$ man echo
- Recreate
myfirst
andtest
. Put the following data intotest
usingecho
.
Name Height Age Place
Arun 172 18 Delhi
Amy 160 22 NYC
Benny 175 35 LA
Xavier 165 15 Egypt
Mani 170 23 Florida
grep¶
Searches file for specified string or expression.
grep "hello" file
- gives the lines that containhello
.grep -i "hello" file
- search for the string case insensitively.grep -c "hello" file
- prints the number of lines which has the string.grep -v "hello" file
- displays the line that are not matched.grep "^hello" file
- matches the lines that start with the string.grep "hello$" file
- matched the lines that end with the string.
For more information,
$ man grep
- Find lines that start with
A
fromtest
file. (Use|
)
tr¶
Used for deleting or translating characters.
tr -d 'w' file
- removes allw
from the file.tr -s file
- replaces multiple spaces in a string with single space.tr -cd 'w' file
- complement of what other option does. So here removes everything exceptw
.
For more information,
$ man tr
- Print content of
test
with a single space in between words.
cut¶
The cut command is a command for cutting out the sections from each line of files.
cut -b 1,2,3 file.txt
- prints the first 3 bytes of each line in the file.cut -b 2-5 file.txt
- prints the 2nd to 5th bytes of each line.cut -d " " -f 1 file.txt
-d
spefifies the delimiter andf
specifies the field. In this example, 1st word from each line is printed.
For more information,
$ man cut
- Print only the age column from
test
file. Use the output of the above exercise. It will be something likecat test | tr <options> | cut <options>
.
sort¶
Sort command sorts the contents of a text file, line by line.
- Lines starting with a number will appear before lines starting with a letter.
- Lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.
-
Lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase.
-
sort file.txt
- sorts the contents of the file. sort -o out.txt in.txt
- puts the output toout.txt
.sort -r file.txt
- sorts in reverse order.sort -n file.txt
- sorts the files in numeric order.sort -u file.txt
- removes duplicate elements.
For more information,
$ man sort
- Print the sorted age from
test
. Use the output of above command. - Print the sorted name from
test
.
man¶
Displays a manual page for the command.
man command
- displays a manual page about thecommand
.
For more information,
$ man man
pwd¶
pwd
- prints the absolute path of the current directory.
For more information,
$ man pwd
clear¶
clear
- clears the teminal screen and put the cursor back at the top of the window.
For more information,
$ man clear
find & locate¶
Used for locating a file/directory.
locate
- is fast way to locate files and uses a recorded snapshot of the system and might miss recent additions.find
- can be also used to find files but might take some time depending on the size of file system.
For more information,
$ man locate
$ man find
ssh¶
Used for logging into a remote machine.
ssh sample.ssh.com
- connects to a remote computer calledsample.ssh.com
.ssh [email protected]
- can be used to log in as a differentuser
.ssh [email protected] -p port
- can be used to specify the port to connect to.
For more information,
$ man ssh
scp¶
scp copies files between hosts on a network.
-r
- can be used for copying a directory recursively.
Remote to local machine:
scp [email protected]:file_on_remote.txt folder_on_local
Local to remote machine:
scp /path/file_on_local [email protected]:folder_on_server
For more information,
$ man scp
Note
For ssh and scp, you might have to provide ssh key using -i key
.
env¶
Prints the defined environment variables.
xxd¶
Prints out the hex dump of the given file.
Usage¶
$ xxd file.txt
00000000: 4865 6c6c 6f20 576f 726c 6421 0a Hello World!.
For more information,
$ man xxd
strings¶
Print the sequences of printable characters which are aleast 4 characters long in file.
Usage¶
$ strings file.txt
For more information,
$ man strings