w3resource
gallery w3resource

Linux commands for intermediate users

 

Once you become accustomed with using Linux, these commands will be of immense help.

Transcript

Linux commands for intermediate users

Once you become accustomed with using Linux, these commands will be of immense help

Command: Find

Search for files in the given directory, hierarchically starting at the parent
directory and moving to sub-directories.
datasoft @ datasoft-linux ~$ find -name '*.sh'
./runyou.sh
./pqr.sh
./psr-for-loop.sh
./psr2.sh
./psr-for-loop1.sh
./pqr1.sh
./.local/share/Trash/files/sqlite-source/sqlite-autoconf-3080500/ltmain.sh
./.local/share/Trash/files/sqlite/sqlite-autoconf-3080500/ltmain.sh
./psr-script-parameter.sh
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 filename to customise the output).
guest-O9kjm6@datasoft-linux:/dev$ find -iname *.SH ( find -iname *.Sh / find -iname *.sH)
bash: syntax error near unexpected token `('
This command searches for all the file having extension ‘tar.gz‘ in root directory and all the sub-directories including mounted devices.
guest-O9kjm6@datasoft-linux:/dev$ find -name *.tar.gz

Command: grep

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

datasoft @ datasoft-linux ~$ grep
Usage: grep [OPTION]... PATTERN [FILE]...
guest-O9kjm6@datasoft-linux:/dev$ grep datasoft /etc/passwd

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

guest-O9kjm6@datasoft-linux:/dev$ grep -i datasoft /etc/passwd

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

guest-O9kjm6@datasoft-linux:/dev$ grep -r "127.0.0.1" /etc/
grep: /etc/mtab.fuselock: Permission denied
grep: /etc/subuid-: Permission denied
grep: /etc/shadow-: Permission denied
/etc/speech-dispatcher/modules/ivona.conf:#IvonaServerHost "127.0.0.1"
grep: /etc/gshadow-: Permission denied
grep: /etc/chatscripts: Permission denied

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,

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 ...] …
…………………………...

Command: ps

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

datasoft @ datasoft-linux
~$ ps
PID TTYTIME
CMD
2745 pts/200:00:00 bash
4306 pts/200:00:00 ps

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

guest-sn9rvF@datasoft-linux:~$ ps -A
PID TTY TIME CMD
1 ?00:00:01 init
2 ?00:00:00 kthreadd
3 ?00:00:00 ksoftirqd/0
5 ?00:00:00 kworker/0:0H
6 ?00:00:01 kworker/u4:0

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

guest-sn9rvF@datasoft-linux:~$ ps -A | grep -i ssh
1909 ? 00:00:00 ssh-agent

Command: kill

This command is used to kill process which is not relevant now or is not responding. It is very useful command, rather a very very useful command. You might be familiar with frequent windows restarting because of the fact that most of the time a running process can’t be killed, and if killed it needs windows to get restart so that changes could be taken into effect but in the world of Linux, there is no such things. Here you can kill a process and start it without restarting the whole system.

guest-sn9rvF@datasoft-linux:~$ ps -A | grep -i apache2
guest-sn9rvF@datasoft-linux:~$

Find process ‘apache2‘, note its pid and kill it. For example, in my case ‘apache2‘ pid is ‘1285‘.Every time you re-run a process or start a system, a new pid is generated for each process and you can know about the current running processes and its pid using command ‘ps‘.

guest-sn9rvF@datasoft-linux:~$ kill 1285 (to kill the process apache2)
bash: syntax error near unexpected token `('

Another way to kill the same process is.

guest-sn9rvF@datasoft-linux:~$ pkill apache2

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‘.

datasoft @ datasoft-linux ~$ whereis

Usage:
whereis [options] file
Options:
-f <file> define search scope
-b search only binaries
-B <dirs> define binaries lookup p
……………………..
………………..

guest-O9kjm6@datasoft-linux:/dev$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

guest-O9kjm6@datasoft-linux:/dev$ whereis kill
kill: /bin/kill /usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz

Command: service

The ‘service‘ command controls the Starting, Stopping or Restarting of a ‘service‘. This command make it possible to start, restart or stop a service without restarting the system, for the changes to be taken into effect.

guest-O9kjm6@datasoft-linux:/dev$ service apache2 start
apache2: unrecognized service

guest-O9kjm6@datasoft-linux:/dev$ service apache2 restart
apache2: unrecognized service

guest-O9kjm6@datasoft-linux:/dev$ service apache2 stop
apache2: unrecognized service

Command: alias

alias is a built in shell command that lets you assign name for a long command or frequently used command

datasoft @ datasoft-linux ~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal ||
echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
…………………….
…………….

guest-sn9rvF@datasoft-linux:~$ alias l='ls -l'

check if it works or not.

guest-sn9rvF@datasoft-linux:~$ l
total 284
drwxr-xr-x 2 guest-sn9rvF guest-sn9rvF 40 Aug 20 15:48 Desktop
drwxr-xr-x 2 guest-sn9rvF guest-sn9rvF 40 Aug 20 15:48 Documents
drwxr-xr-x 2 guest-sn9rvF guest-sn9rvF 40 Aug 20 15:48 Downloads

To remove alias ‘l‘, use the following ‘unalias‘ command.
guest-sn9rvF@datasoft-linux:~$ unalias l

check, if ‘l‘ still is alias or not.

guest-sn9rvF@datasoft-linux:~$ l
l: command not found

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.

datasoft @ datasoft-linux ~$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda9 46814160 3989124 40423964 9% /
none 4 0 4 0% /sys/fs/cgroup
udev 957868 4 957864 1% /dev
tmpfs 193480 1048 192432 1% /run
…………
…………..

Command: du

Estimate file space usage. Output the summary of disk usages by ever file hierarchically, i.e., in recursive manner.

16 ./.cache/ibus/bus
16 ./.cache/ibus
64 ./.cache/upstart
0 ./.cache/unity
20884 ./.cache
8 ./.config/gedit
4 ./.config/gtk-2.0
16 ./.config/nautilus
12 ./.config/libaccounts-glib
4 ./.config/compiz-1/compizconfig
4 ./.config/compiz-1

Command: rm

The command ‘rm‘ stands for remove. rm is used to remove files (s) and directories.

datasoft @ datasoft-linux ~$ rm PassportApplicationForm_Main_English_V1.0 rm: cannot remove ‘PassportApplicationForm_Main_English_V1.0’: No such file or directory

The directory can’t be removed simply by ‘rm‘ command, you have to use ‘-rf‘ switch along with ‘rm‘.

guest-sn9rvF@datasoft-linux:~$ rm -rf PassportApplicationForm_Main_Engli

Command: echo

echo as the name suggest echoes a text on the standard output. It has nothing to do with shell, nor does shell reads the output of echo command. However in an interactive script, echo passes the message to the user through terminal. It is one of the command that is commonly used in scripting, interactive scripting.

guest-sn9rvF@datasoft-linux:~$ echo "w3resource.com provides you with lots of excellent content"
w3resource.com provides you with lots of excellent content
guest-sn9rvF@datasoft-linux:~$

Command: echo

creating a small interactive script
1. create a file, named ‘interactive_shell.sh‘ on desktop. (Remember ‘.sh‘ extension is must).
2. copy and paste the below script, exactly same, as below.

guest-O9kjm6@datasoft-linux:~$ !/bin/bash
bash: !/bin/bash: event not found
guest-O9kjm6@datasoft-linux:~$ echo "Please enter your name:"
Please enter your name:

Next, set execute permission and run the script.

guest-O9kjm6@datasoft-linux:~$ chmod 777 interactive_shell.sh
chmod: cannot access ‘interactive_shell.sh’: No such file or directory

Next, set execute permission and run the script.

guest-O9kjm6@datasoft-linux:~$ ./interactive_shell.sh
bash: ./interactive_shell.sh: No such file or directory

This is an important command that is useful for changing own password in terminal. Obviously you need to know your current password for Security reason.

guest-sn9rvF@datasoft-linux:~$ passwd
Changing password for guest-sn9rvF.
(current) UNIX password:
passwd: Authentication token manipulation error
passwd: password unchanged

Command: lpr

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

guest-sn9rvF@datasoftlinux:~$
lpr -P deskjet-
4620-series 1-final.pdf
lpr: Error - unable to access
"1-final.pdf" - No such file
or directory

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.

guest-sn9rvF@datasoft-linux:~$ cat file1.txt
cat: file1.txt: No such file or directory
guest-sn9rvF@datasoft-linux:~$ cat file2.txt
cat: file2.txt: No such file or directory

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

guest-O9kjm6@datasoft-linux:~$ cmp file1.txt file2.txt
cmp: file1.txt: No such file or directory

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.

guest-sn9rvF@datasoft-linux:~$ wget http://downloads.sourceforge.
net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2
--2014-08-20 16:46:34-- http://downloads.sourceforge.net/project/ffmpegphp/
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………...

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.

datasoft @ datasoft-linux ~$ mount
/dev/sda9 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/cgroup type tmpfs (rw)
none on /sys/fs/fuse/connections type fusectl (rw)
……………….
…………………...

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

guest-O9kjm6@datasoft-linux:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
├─sda1 8:1 0 73.2G 0 part
├─sda2 8:2 0 1K 0 part
├─sda5 8:5 0 97.7G 0 part
├─sda6 8:6 0 97.7G 0 part
├─sda7 8:7 0 97.7G 0 part
………………………….
……………………….

From previous 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.

guest-O9kjm6@datasoft-linux:~$ su
Password:
su: Authentication failure
guest-O9kjm6@datasoft-linux:~$ cd /dev

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

guest-O9kjm6@datasoft-linux:/dev$ mkdir usb
mkdir: cannot create directory ‘usb’: Permission denied

Now mount filesystem ‘sdb1‘ to directory ‘usb‘.you can navigate to /dev/usb from terminal or X-windows system and access file from the mounted directory.

guest-O9kjm6@datasoft-linux:/dev$ mount /dev/sdb1 /dev/usb
mount: only root can do that

Command: gcc

gcc is the in-built compiler for ‘c‘ language in Linux Environment. A simple c program, save it on your desktop as Hello.c (remember ‘.c‘ extension is must).

guest-O9kjm6@datasoft-linux:~$ include <stdio.h>
bash: syntax error near unexpected token `newline'
guest-O9kjm6@datasoft-linux:~$ int main()
bash: syntax error near unexpected token `('

guest-O9kjm6@datasoft-linux:~$ gcc Hello.c
gcc: error: Hello.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.

guest-O9kjm6@datasoft-linux:~$ ./a.out
bash: ./a.out: No such file or directory

On compiling a c program the output is automatically generated to a new file “a.out” and everytime you compile a c program same file “a.out” gets modified. Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.Compile it this way:-Here ‘-o‘ sends the output to ‘Hello‘ file and not ‘a.out‘. Run it again.

guest-O9kjm6@datasoft-linux:~$ gcc -o Hello Hello.c
gcc: error: Hello.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.

On compiling a c program the output is automatically generated to a new file “a.out” and everytime you compile a c program same file “a.out” gets modified. Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.Here ‘-o‘ sends the output to ‘Hello‘ file and not ‘a.out‘. Run it again.

guest-O9kjm6@datasoft-linux:~$ ./Hello
bash: ./Hello: No such file or directory

Command: g++

g++ is the in-built compiler for ‘C++‘ , the first object oriented programming language. A simple c++ program, save it on your desktop as Add.cpp (remember ‘.cpp‘ extension is must).

guest-sn9rvF@datasoftlinux:~$
g++ Add.cpp
g++: error: Add.cpp: No
such file or directory
g++: fatal error: no input
files
compilation terminated.

On compiling a c++ program the output is automatically generated to a new file “a. out” and everytime you compile a c++ program same file “a.out” gets modified.

guest-O9kjm6@datasoft-linux:/dev$ ./a.out
bash: ./a.out: No such file or directory

Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.compile this way:-

guest-O9kjm6@datasoft-linux:/dev$ g++ -o Add Add.cpp
g++: error: Add.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.

Run it

guest-O9kjm6@datasoft-linux:/dev$ ./Add
bash: ./Add: No such file or directory

Command: java

Java is one of the world’s highly used programming language and is considered fast, secure, and reliable. Most of the the web based service of today runs on java.

datasoft @ datasoft-linux ~$ java
The program 'java' can be found in the following packages:
* default-jre
* gcj-4.8-jre-headless
* openjdk-7-jre-headless
* gcj-4.6-jre-headless
* openjdk-6-jre-headless
Try: sudo apt-get install <selected package>

Create a simple java program by pasting the below test to a file, named datasoft. java (remember ‘.java‘ extension is must).

guest-O9kjm6@datasoft-linux:/dev$ javac datasoft.java
The program 'javac' can be found in the following packages:
* default-jdk
* ecj
* gcj-4.8-jdk
* openjdk-7-jdk
* gcj-4.6-jdk

guest-O9kjm6@datasoft-linux:/dev$ java datasoft
The program 'java' can be found in the following packages:
* default-jre
* gcj-4.8-jre-headless
* openjdk-7-jre-headless
* gcj-4.6-jre-headless
* openjdk-6-jre-headless
Ask your administrator to install one of them



Follow us on Facebook and Twitter for latest update.