Linux Commands and Shell Scripts

Top 10 Advanced Commands for Middle Level Linux Users

 

1.Command: Find

Search for files in the given directory, hierarchically starting at the parent directory and moving to sub-directories.

[email protected]:~# find -name *.sh
./Desktop/load.sh
./Desktop/test.sh
./Desktop/shutdown.sh
./Binary/firefox/run-mozilla.sh
./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh
./Downloads/kdewebdev-3.5.8/admin/doxygen.sh
./Downloads/kdewebdev-3.5.8/admin/cvs.sh
./Downloads/kdewebdev-3.5.8/admin/ltmain.sh
./Downloads/wheezy-nv-install.sh

 

Note: The `-name‘ option makes the search case sensitive. You can use the `-iname‘ option to find something regardless of case. (* is a wildcard and searches all the file having extension ‘.sh‘ you can use filename or a part of the file name to customize the output).

[email protected]:~# find -iname *.SH ( find -iname *.Sh / find -iname *.sH)

./Desktop/load.sh
./Desktop/test.sh
./Desktop/shutdown.sh
./Binary/firefox/run-mozilla.sh
./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh
./Downloads/kdewebdev-3.5.8/admin/doxygen.sh
./Downloads/kdewebdev-3.5.8/admin/cvs.sh
./Downloads/kdewebdev-3.5.8/admin/ltmain.sh
./Downloads/wheezy-nv-install.sh

[email protected]:~# find -name *.tar.gz

/var/www/modules/update/tests/aaa_update_test.tar.gz
./var/cache/flashplugin-nonfree/install_flash_player_11_linux.i386.tar.gz
./home/server/Downloads/drupal-7.22.tar.gz
./home/server/Downloads/smtp-7.x-1.0.tar.gz
./home/server/Downloads/noreqnewpass-7.x-1.2.tar.gz
./usr/share/gettext/archive.git.tar.gz
./usr/share/doc/apg/php.tar.gz
./usr/share/doc/festival/examples/speech_pm_1.0.tar.gz
./usr/share/doc/argyll/examples/spyder2.tar.gz
./usr/share/usb_modeswitch/configPack.tar.gz

Note: The above command searches for all the file having extension ‘tar.gz‘ in root directory and all the sub-directories including mounted devices.

2.Command: grep

The ‘grep‘ command searches the given file for lines containing a match to the given strings or words. Search ‘/etc/passwd‘ for ‘tecmint‘ user.

[email protected]:~# grep copycookie /etc/passwd

copycookie:x:1000:1000:copycookie,,,:/home/copycookie:/bin/bash

Ignore word case and all other combination with ‘-i‘ option.

[email protected]:~# grep -i copycookie /etc/passwd

copycookie:x:1000:1000:copycookie,,,:/home/copycookie:/bin/bash

Search recursively (-r) i.e. read all files under each directory for a string “127.0.0.1“.

[email protected]:~# grep -r “127.0.0.1” /etc/

/etc/vlc/lua/http/.hosts:127.0.0.1
/etc/speech-dispatcher/modules/ivona.conf:#IvonaServerHost “127.0.0.1”
/etc/mysql/my.cnf:bind-address = 127.0.0.1
/etc/apache2/mods-available/status.conf: Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/ldap.conf: Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/info.conf: Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/proxy_balancer.conf:# Allow from 127.0.0.1 ::1
/etc/security/access.conf:#+ : root : 127.0.0.1
/etc/dhcp/dhclient.conf:#prepend domain-name-servers 127.0.0.1;
/etc/dhcp/dhclient.conf:# option domain-name-servers 127.0.0.1;
/etc/init/network-interface.conf: ifconfig lo 127.0.0.1 up || true
/etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).
/etc/java-6-openjdk/net.properties:# http.nonProxyHosts=localhost|127.0.0.1
/etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).
/etc/java-6-openjdk/net.properties:# ftp.nonProxyHosts=localhost|127.0.0.1
/etc/hosts:127.0.0.1 localhost

Note: You can use these following options along with grep.

-w for word (egrep -w ‘word1|word2‘ /path/to/file).
-c for count (i.e., total number of times the pattern matched) (grep -c ‘word‘ /path/to/file).
–color for coloured output (grep –color server /etc/passwd).

3.Command: man

The ‘man‘ is the system’s manual pager. Man provides online documentation for all the possible options with a command and its usages. Almost all the command comes with their corresponding manual pages. For example,

[email protected]:~# man man

MAN(1) Manual pager utils MAN(1)

NAME
man – an interface to the on-line reference manuals

SYNOPSIS
man [-C file] [-d] [-D] [–warnings[=warnings]] [-R encoding] [-L locale] [-m system[,…]] [-M path] [-S list] [-e extension] [-i|-I]
[–regex|–wildcard] [–names-only] [-a] [-u] [–no-subpages] [-P pager] [-r prompt] [-7] [-E encoding] [–no-hyphenation] [–no-justification] [-p
string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section] page …] …
man -k [apropos options] regexp …
man -K [-w|-W] [-S list] [-i|-I] [–regex] [section] term …
man -f [whatis options] page …
man -l [-C file] [-d] [-D] [–warnings[=warnings]] [-R encoding] [-L locale] [-P pager] [-r prompt] [-7] [-E encoding] [-p string] [-t] [-T[device]]
[-H[browser]] [-X[dpi]] [-Z] file …
man -w|-W [-C file] [-d] [-D] page …
man -c [-C file] [-d] [-D] page …
man [-hV]

4.Command: ps

ps (Process) gives the status of running processes with a unique Id called PID.

[email protected]:~# ps

PID TTY TIME CMD
4170 pts/1 00:00:00 bash
9628 pts/1 00:00:00 ps

To list status of all the processes along with process id and PID, use option ‘-A‘.

[email protected]:~# ps -A

PID TTY TIME CMD
1 ? 00:00:01 init
2 ? 00:00:00 kthreadd
3 ? 00:00:01 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
7 ? 00:00:00 kworker/u:0H
8 ? 00:00:00 migration/0
9 ? 00:00:00 rcu_bh
….

Note: This command is very useful when you want to know which processes are running or may need PID sometimes, for process to be killed. You can use it with ‘grep‘ command to find customised output. For example,

[email protected]:~#ps -A | grep -i ssh

1500 ? 00:09:58 sshd
4317 ? 00:00:00 sshd

Here ‘ps‘ is pipelined with ‘grep‘ command to find customized and relevant output of our need.

5.Command: whereis

The ‘whereis‘ command is used to locate the Binary, Sources and Manual Pages of the command. For example, to locate the Binary, Sources and Manual Pages of the command ‘ls‘ and ‘kill‘.

[email protected]:~# whereis ls

ls: /bin/ls /usr/share/man/man1/ls.1.gz

[email protected]:~# whereis kill

kill: /bin/kill /usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz

Note: This is useful to know where the binaries are installed for manual editing sometimes.

6.Command: df

Report disk usages of file system. Useful for user as well as System Administrator to keep track of their disk usages. ‘df‘ works by examining directory entries, which generally are updated only when a file is closed.

[email protected]:~# df

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 47929224 7811908 37675948 18% /
none 4 0 4 0% /sys/fs/cgroup
udev 1005916 4 1005912 1% /dev
tmpfs 202824 816 202008 1% /run
none 5120 0 5120 0% /run/lock
none 1014120 628 1013492 1% /run/shm
none 102400 44 102356 1% /run/user
/dev/sda5 184307 79852 94727 46% /boot
/dev/sda7 95989516 61104 91045676 1% /data
/dev/sda8 91953192 57032 87218528 1% /personal

7.Command: lpr

This command print files named on command line, to named printer.

[email protected]:~# lpr -P deskjet-4620-series 1-final.pdf

Note: The ‘lpq‘ command lets you view the status of a printer (whether it’s up or not), and the jobs (files) waiting to be printed.

8.Command: cmp

compare two files of any type and writes the results to the standard output. By default, ‘cmp‘ Returns 0 if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported.

To provide examples for this command, lets consider two files:

file1.txt

[email protected]:~# cat file1.txt

Hi My name is copycookie

file2.txt

[email protected]:~# cat file2.txt

Hi My name is copycookie [dot] com

Now, let’s compare two files and see output of the command.

[email protected]:~# cmp file1.txt file2.txt

file1.txt file2.txt differ: byte 15, line 1

 

9.Command: mount

Mount is an important command which is used to mount a filesystem that don’t mount itself. You need root permission to mount a device.

First run ‘lsblk‘ after plugging-in your filesystem and identify your device and note down you device assigned name.

[email protected]:~# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 923.6G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 7.9G 0 part [SWAP]
sr0 11:0 1 1024M 0 rom
sdb 8:16 1 3.7G 0 disk
└─sdb1 8:17 1 3.7G 0 part

From this screen it was clear that I plugged in a 4 GB pendrive thus ‘sdb1‘ is my filesystem to be mounted. Become a root to perform this operation and change to /dev directory where all the file system is mounted.

[email protected]:~# su
Password:

[email protected]:~# cd /dev

Create a directory named anything but should be relevent for reference.

[email protected]:~# mkdir usb
Now mount filesystem ‘sdb1‘ to directory ‘usb‘.

[email protected]:~# mount /dev/sdb1 /dev/usb

Now you can navigate to /dev/usb from terminal or X-windows system and acess file from the mounted directory.

10.Command: wget

Wget is a free utility for non-interactive (i.e., can work in background) download of files from the Web. It supports HTTP, HTTPS, FTP protocols and HTTP proxies.

Download ffmpeg using wget

[email protected]:~# wget http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2

–2020-10-11 10:55:00– http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2
Resolving downloads.sourceforge.net (downloads.sourceforge.net)… 216.34.181.59
Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80… connected.
HTTP request sent, awaiting response… 302 Found
Location: http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 [following]
–2020-10-11 10:55:00– http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2
Resolving kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)… 92.46.53.163
Connecting to kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)|92.46.53.163|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 275557 (269K) [application/octet-stream]
Saving to: ‘ffmpeg-php-0.6.0.tbz2’

100%[===========================================================================>] 2,75,557 67.8KB/s in 4.0s

2020-10-11 10:55:00 (67.8 KB/s) – ‘ffmpeg-php-0.6.0.tbz2’ saved [275557/275557]

 

 

Related Articles