Commands
Commands
Commands
at
Schedule a command to run once at a particular time,
awk
Find and Replace text
● From an ls - l listing, return the fifth item ($5) from each line
of the output:
● Print the Row Number (NR), then a dash and space ("- ") and
then the first item ($1) from each line in samplefile.txt:
● Print the first item ($1) and then the third last item $(NF-2)
from each line in samplefile.txt:
$ awk '{print $1, $(NF-2) }' samplefile.txt
● Print every line that has at least one field.
awk 'NF > 0' data.txt
bg
Sends a job/process/task in the background
bash
Open a new instance of the bash shell.
base32
Base32 encode/decode data and print to standard output.
cd
Change Directory - change the current working directory to a specific Folder.
● Move to the sybase folder: $ cd /usr/local/sybase
$ pwd
/usr/local/sybase
$ pwd
/var/log
$ pwd
/usr/local/sybase
$ pwd
/usr/local/
chmod
Examples
cat
Concatenate and print (display) the content of files.
cut
Divide a file into several parts (columns)
Cut from a single string -> echo "Lorem ipsum dolor sit amet"
| cut -d ' ' -f 2 [output: ipsum]
Cut from a file -> to display the 1st and 3rd fields using “:” as
a delimiter:
cut test.txt -d ':' -f 1,3
Parse out column 2 from a semicolon (;) delimited file:
$ cat myfile.txt | cut -d ‘;’ -f 2 > output.txt
cp [ source destination]
copy files and directories
$ cp *.txt newsportal/
df
Disk Free - display free disk space.
dir
Briefly list directory contents.
Syntax
Dir [Equivalent to 'ls -C -b']
diff
Display the differences between two files
Note the files have to be sorted first (the order matters) and if the
files could contain duplicate values, then the output of sort has to
be run through the uniq command to eliminate any duplicate
elements.
Echo
Display message on screen, writes each given STRING to standard
output, with a space between each and a newline after the last one.
exit
Exit from a program, shell or log out of a Unix network.
Syntax
exit [n]
Key
n Set the exit status to n (default=0)
expr
Evaluate expressions, evaluates an expression and writes the result
on standard output.
Examples
# Adding numbers
$ expr 5 + 2
7
find
The find command in UNIX is a command line utility for walking a
file hierarchy. It can be used to find files and directories and
perform subsequent operations on them. It supports searching by
file, folder, name, creation date, modification date, owner and
permissions. By using the ‘-exec’ other UNIX commands can be
executed on files or folders found.
Search a file with pattern: $ find ./GFG -name *.txt [It will give
all files which have ‘.txt’ at the end.]
Search a file with specific name: $ find ./GFG -name
sample.txt [It will search for sample.txt in GFG directory.]
How to find and delete a file with confirmation: $ find ./GFG -
name sample.txt -exec rm -i {} \;
Search for empty files and directories: $ find ./GFG –empty
[This command find all empty folders and files in the entered
directory or sub-directories.]
fg
Sends a job/process/task in the foreground
Fuser
Identify processes which are using files or sockets, optionally: Kill
the process that is accessing the file.
grep
Search file(s) for specific text.
● Search the file example.txt, including binary data (-a) for the string 'hunting the
snark':
$ grep -a “hunting the snark” example.txt
● Search the whole partition (/sda1), including binary data(-a) for the string 'hunting
the snark' return all the lines starting 25 Before the text found and 50 lines After
the matching text found.
This can be a way to discover fragments of deleted files but is very slow:
$ grep -a -B 25 -A 50 “hunting the snark” /dev/sda1 >
results.txt
● Matching the lines that start with a string: The ^ regular expression pattern
specifies the start of a line. This can be used in grep to match the lines which
start with the given string or pattern.
$ grep "^unix" geekfile.txt
Matching the lines that end with a string: The $ regular expression pattern
specifies the end of a line. This can be used in grep to match the lines which end
with the given string or pattern.
gzip
Compress or decompress named file(s)
gzip filename
This will compress the file, and append a .gz extension to it. The
original file is deleted. To prevent this, you can use the -c option
and use output redirection to write the output to the filename.gz
file:
gzip -c filename > filename.gz [The -c option specifies that
output will go to the standard output stream, leaving the
original file intact]
You can compress multiple files by listing them: gzip
filename1 filename2
You can compress all the files in a directory, recursively, using
the -r option: gzip -r a_folder
gzip can also be used to decompress a file, using the -d
option: gzip -d filename.gz
head
output the first part of files, prints the first part (10 lines by
default) of each file.
Extract the first 85 lines from a file: head –n 85 file.txt
Extract lines 40-50 from a file, first using head to get the first
50 lines then tail to get the last 10: head -50 file.txt | tail -10
history
Command Line history.
ifconfig
Interface configurator
Change the state of the network interface device(s) to UP:
$ ifconfig eth0 up
Change the state of the network interface device(s) to DOWN:
(see also ip link):
$ ifconfig eth0 dn
ip
show all IP addresses associated on all network devices :
$ ip address
ifup / ifdown
Bring a network interface up or down
Bring up all the interfaces defined with auto in /etc/network/interfaces :
ifup -a
ifup eth0
ifdown -a
Jobs
Check background processes
killall
Kill processes by name.
ls
List information about files.
mv
Move or rename files or directories.
mkdir
Create new folder(s), if they do not already exist.
Create the folder 'demo': $ mkdir demo
pwd
Print Working Directory.
rm
Remove files (delete/unlink).
$ rmdir myfolder
sort
Sort text files.
-k, --key=POS1[,POS2]
Start a key at POS1 (origin 1), end it at POS2 (default end of line).
tail
Output the last part of files, print the last part (10 lines by default)
of each FILE.
&
Starts a new job/process in the background.