Hello everyone. This is my first post for the year, and I thought I’d share some useful linux commands that every frequent Linux user must be comfortable with
They’ve been written in the form of short notes, because that’s the way I took them down at the FOSS Workshop at IIT Madras. Have Fun!
directory user group permission
d
directory
r
read
w
writeable
x
executable
chmod change file modes / access control list o others u user g group
Ex:
chmod o+w filename.exe
: add w flag
chmod o-w filename.exe
: remove w flag
chmod o-r,u-r,g-r filename.exe
: change permissions of others, user,grp
$ touch
changes the timestamp of the file. Used to create new file
i
to go to insert mode, wherein you can edit text in the fileEsc
. Now you will be in command mode:wq
. To quit without saving, :q!
>
, which implies direction. Ex: $ echo “Out” > test.data>>
instead of >
You can output any terminal command to a file using >
. For instance, ls -l > test.dat
|
)$ echo "pipe" | cat
You can search for an expression in something. Ex: If you have a file demo.dat
, and you want to search for Chennai, then
cat "demo.dat" | grep "Chennai"
If you want it to be case insensitive, use the ignore case
flag given by -i
cat "demo.dat" | grep -i "Chennai"
The -v
flag will exclude from the results the regex. So, below, it’ll print all lines except the ones containing Chennai
cat "demo.dat" | grep -v "Chennai"
The -o
flag will only show the match, not the entire line that contains it. Ex:
cat "demo.dat" | grep -i -o "chennai"
will output Chennai
as the match is case insensitive, and it has to display only the match
GREP recursive search - search entire directory for file. Indicate with the flag -R
. You can also indicate line number of the result using the -n
Ex: If we have a folder test_folder
grep -nR "Chennai" test_folder
(You can group -n -R
to -nR
)
The biggest advantage of pipes are in the fact that you can keep piping output of one command as input of the next. For instance, the last awk command below is to be greped, and only ‘chennai is needed’
cat demo.dat | awk '($4>27 && $3<35){print $1,$3}' > myOutput.txt | grep -i "chennai"
Can be used for terminal spreadsheet management and display
Ex: You have a tab separated datasheet demo.dat
, and we want to print only the first two columns
cat demo.dat" | awk '{print $1,$2;}'
You can also add conditions (like if conditions) so that you will output only those lines which satisfy the conditions. To do this, add the condition in ()
preceeding the {}
within the single quotes
cat demo.dat | awk '($4>27 && $3<35){print $1,$3}'
will print only those records where the fourth column is greater than 27 AND the 3rd column is greater than 35
The advantage of piping is that you can cascade them. For instance, in the above command, if we want to put the output to some file
cat demo.dat | awk '($4>27 && $3<35){print $1,$3}' > myOutput.txt
-n
flag if you want to interpret the sort basis as a number and not a string-k
. The key is the column it will use to sort-n
Ex:
cat demo.dat | sort -n -k 3
head
and tail
command-n
to specify the number of linesEx: Display the top two results from above
cat demo.dat | sort -n -k 3 | head -n 2
wc
or wc -l
)Ex: Count number of records above
cat demo.dat | sort -n -k 3 | wc -l
sed
commandEx: Seach for Chennai and replace all occurences with Madras (Indicated by /g
)
cat demo.dat | sed 's/Chennai/Madras/g'
-d
Can use only a single character as delimiter. We can also specify which field we want. Ex: we want only the usernames
cat emails.dat | cut -d '@' -f 1
Suppose we want to see the email is of a .com or .org or .in, then,,
cat emails.dat | cut -d '@' -f 2 |cut -d '.' -f 2
#!/bin/bash
$1
: First argument. $2
: Second argument#!/bin/bash
in="$1"
output = "$2"
# printing united cities
cat "$in" |grep -i "united" > "$output"
echo "printed united"
cat "$in" |grep -i "africA" >> "$output"
echo "Completed"
Simple use eog
. Ex: eog as.jpg
will open as.jpg
with your default image viewer