· 4 years ago · Jan 27, 2021, 03:38 PM
1LINUX NOT SO QUICK REF // original post: https://www.thegeekstuff.com/2010/11/50-linux-commands/
2
31. tar command examples
4
5Create a new tar archive.
6
7$ tar cvf archive_name.tar dirname/
8
9Extract from an existing tar archive.
10
11$ tar xvf archive_name.tar
12
13View an existing tar archive.
14
15$ tar tvf archive_name.tar
16
17More tar examples: The Ultimate Tar Command Tutorial with 10 Practical Examples
182. grep command examples
19
20Search for a given string in a file (case in-sensitive search).
21
22$ grep -i "the" demo_file
23
24Print the matched line, along with the 3 lines after it.
25
26$ grep -A 3 -i "example" demo_text
27
28Search for a given string in all files recursively
29
30$ grep -r "ramesh" *
31
32More grep examples: Get a Grip on the Grep! – 15 Practical Grep Command Examples
333. find command examples
34
35Find files using file-name ( case in-sensitve find)
36
37# find -iname "MyCProgram.c"
38
39Execute commands on files found by the find command
40
41$ find -iname "MyCProgram.c" -exec md5sum {} \;
42
43Find all empty files in home directory
44
45# find ~ -empty
46
47More find examples: Mommy, I found it! — 15 Practical Linux Find Command Examples
484. ssh command examples
49
50Login to remote host
51
52ssh -l jsmith remotehost.example.com
53
54Debug ssh client
55
56ssh -v -l jsmith remotehost.example.com
57
58Display ssh client version
59
60$ ssh -V
61OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
62
63More ssh examples: 5 Basic Linux SSH Client Commands
645. sed command examples
65
66When you copy a DOS file to Unix, you could find \r\n in the end of each line. This example converts the DOS file format to Unix file format using sed command.
67
68$sed 's/.$//' filename
69
70Print file content in reverse order
71
72$ sed -n '1!G;h;$p' thegeekstuff.txt
73
74Add line number for all non-empty-lines in a file
75
76$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'
77
78More sed examples: Advanced Sed Substitution Examples
796. awk command examples
80
81Remove duplicate lines using awk
82
83$ awk '!($0 in array) { array[$0]; print }' temp
84
85Print all lines from /etc/passwd that has the same uid and gid
86
87$awk -F ':' '$3==$4' passwd.txt
88
89Print only specific field from a file.
90
91$ awk '{print $2,$5;}' employee.txt
92
93More awk examples: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
947. vim command examples
95
96Go to the 143rd line of file
97
98$ vim +143 filename.txt
99
100Go to the first match of the specified
101
102$ vim +/search-term filename.txt
103
104Open the file in read only mode.
105
106$ vim -R /etc/passwd
107
108More vim examples: How To Record and Play in Vim Editor
1098. diff command examples
110
111Ignore white space while comparing.
112
113# diff -w name_list.txt name_list_new.txt
114
1152c2,3
116< John Doe --- > John M Doe
117> Jason Bourne
118
119More diff examples: Top 4 File Difference Tools on UNIX / Linux – Diff, Colordiff, Wdiff, Vimdiff
1209. sort command examples
121
122Sort a file in ascending order
123
124$ sort names.txt
125
126Sort a file in descending order
127
128$ sort -r names.txt
129
130Sort passwd file by 3rd field.
131
132$ sort -t: -k 3n /etc/passwd | more
133
13410. export command examples
135
136To view oracle related environment variables.
137
138$ export | grep ORACLE
139declare -x ORACLE_BASE="/u01/app/oracle"
140declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
141declare -x ORACLE_SID="med"
142declare -x ORACLE_TERM="xterm"
143
144To export an environment variable:
145
146$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0
147
14811. xargs command examples
149
150Copy all images to external hard-drive
151
152# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
153
154Search all jpg images in the system and archive it.
155
156# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
157
158Download all the URLs mentioned in the url-list.txt file
159
160# cat url-list.txt | xargs wget –c
161
16212. ls command examples
163
164Display filesize in human readable format (e.g. KB, MB etc.,)
165
166$ ls -lh
167-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz
168
169Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr
170
171$ ls -ltr
172
173Visual Classification of Files With Special Characters Using ls -F
174
175$ ls -F
176
177More ls examples: Unix LS Command: 15 Practical Examples
17813. pwd command
179
180pwd is Print working directory. What else can be said about the good old pwd who has been printing the current directory name for ages.
18114. cd command examples
182
183Use “cd -” to toggle between the last two directories
184
185Use “shopt -s cdspell” to automatically correct mistyped directory names on cd
186
187More cd examples: 6 Awesome Linux cd command Hacks
18815. gzip command examples
189
190To create a *.gz compressed file:
191
192$ gzip test.txt
193
194To uncompress a *.gz file:
195
196$ gzip -d test.txt.gz
197
198Display compression ratio of the compressed file using gzip -l
199
200$ gzip -l *.gz
201 compressed uncompressed ratio uncompressed_name
202 23709 97975 75.8% asp-patch-rpms.txt
203
20416. bzip2 command examples
205
206To create a *.bz2 compressed file:
207
208$ bzip2 test.txt
209
210To uncompress a *.bz2 file:
211
212bzip2 -d test.txt.bz2
213
214More bzip2 examples: BZ is Eazy! bzip2, bzgrep, bzcmp, bzdiff, bzcat, bzless, bzmore examples
21517. unzip command examples
216
217To extract a *.zip compressed file:
218
219$ unzip test.zip
220
221View the contents of *.zip file (Without unzipping it):
222
223$ unzip -l jasper.zip
224Archive: jasper.zip
225 Length Date Time Name
226 -------- ---- ---- ----
227 40995 11-30-98 23:50 META-INF/MANIFEST.MF
228 32169 08-25-98 21:07 classes_
229 15964 08-25-98 21:07 classes_names
230 10542 08-25-98 21:07 classes_ncomp
231
23218. shutdown command examples
233
234Shutdown the system and turn the power off immediately.
235
236# shutdown -h now
237
238Shutdown the system after 10 minutes.
239
240# shutdown -h +10
241
242Reboot the system using shutdown command.
243
244# shutdown -r now
245
246Force the filesystem check during reboot.
247
248# shutdown -Fr now
249
25019. ftp command examples
251
252Both ftp and secure ftp (sftp) has similar commands. To connect to a remote server and download multiple files, do the following.
253
254$ ftp IP/hostname
255ftp> mget *.html
256
257To view the file names located on the remote server before downloading, mls ftp command as shown below.
258
259ftp> mls *.html -
260/ftptest/features.html
261/ftptest/index.html
262/ftptest/othertools.html
263/ftptest/samplereport.html
264/ftptest/usage.html
265
266More ftp examples: FTP and SFTP Beginners Guide with 10 Examples
26720. crontab command examples
268
269View crontab entry for a specific user
270
271# crontab -u john -l
272
273Schedule a cron job every 10 minutes.
274
275*/10 * * * * /home/ramesh/check-disk-space
276
277More crontab examples: Linux Crontab: 15 Awesome Cron Job Examples
27821. service command examples
279
280Service command is used to run the system V init scripts. i.e Instead of calling the scripts located in the /etc/init.d/ directory with their full path, you can use the service command.
281
282Check the status of a service:
283
284# service ssh status
285
286Check the status of all the services.
287
288service --status-all
289
290Restart a service.
291
292# service ssh restart
293
29422. ps command examples
295
296ps command is used to display information about the processes that are running in the system.
297
298While there are lot of arguments that could be passed to a ps command, following are some of the common ones.
299
300To view current running processes.
301
302$ ps -ef | more
303
304To view current running processes in a tree structure. H option stands for process hierarchy.
305
306$ ps -efH | more
307
30823. free command examples
309
310This command is used to display the free, used, swap memory available in the system.
311
312Typical free command output. The output is displayed in bytes.
313
314$ free
315 total used free shared buffers cached
316Mem: 3566408 1580220 1986188 0 203988 902960
317-/+ buffers/cache: 473272 3093136
318Swap: 4000176 0 4000176
319
320If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes.
321
322$ free -g
323 total used free shared buffers cached
324Mem: 3 1 1 0 0 0
325-/+ buffers/cache: 0 2
326Swap: 3 0 3
327
328If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below.
329
330ramesh@ramesh-laptop:~$ free -t
331 total used free shared buffers cached
332Mem: 3566408 1592148 1974260 0 204260 912556
333-/+ buffers/cache: 475332 3091076
334Swap: 4000176 0 4000176
335Total: 7566584 1592148 5974436
336
33724. top command examples
338
339top command displays the top processes in the system ( by default sorted by cpu usage ). To sort top output by any column, Press O (upper-case O) , which will display all the possible columns that you can sort by as shown below.
340
341Current Sort Field: P for window 1:Def
342Select sort field via field letter, type any other key to return
343
344 a: PID = Process Id v: nDRT = Dirty Pages count
345 d: UID = User Id y: WCHAN = Sleeping in Function
346 e: USER = User Name z: Flags = Task Flags
347 ........
348
349To displays only the processes that belong to a particular user use -u option. The following will show only the top processes that belongs to oracle user.
350
351$ top -u oracle
352
353More top examples: Can You Top This? 15 Practical Linux Top Command Examples
35425. df command examples
355
356Displays the file system disk space usage. By default df -k displays output in bytes.
357
358$ df -k
359Filesystem 1K-blocks Used Available Use% Mounted on
360/dev/sda1 29530400 3233104 24797232 12% /
361/dev/sda2 120367992 50171596 64082060 44% /home
362
363df -h displays output in human readable form. i.e size will be displayed in GB’s.
364
365ramesh@ramesh-laptop:~$ df -h
366Filesystem Size Used Avail Use% Mounted on
367/dev/sda1 29G 3.1G 24G 12% /
368/dev/sda2 115G 48G 62G 44% /home
369
370Use -T option to display what type of file system.
371
372ramesh@ramesh-laptop:~$ df -T
373Filesystem Type 1K-blocks Used Available Use% Mounted on
374/dev/sda1 ext4 29530400 3233120 24797216 12% /
375/dev/sda2 ext4 120367992 50171596 64082060 44% /home
376
37726. kill command examples
378
379Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process.
380
381$ ps -ef | grep vim
382ramesh 7243 7222 9 22:43 pts/2 00:00:00 vim
383
384$ kill -9 7243
385
386More kill examples: 4 Ways to Kill a Process – kill, killall, pkill, xkill
38727. rm command examples
388
389Get confirmation before removing the file.
390
391$ rm -i filename.txt
392
393It is very useful while giving shell metacharacters in the file name argument.
394
395Print the filename and get confirmation before removing the file.
396
397$ rm -i file*
398
399Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.
400
401$ rm -r example
402
40328. cp command examples
404
405Copy file1 to file2 preserving the mode, ownership and timestamp.
406
407$ cp -p file1 file2
408
409Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
410
411$ cp -i file1 file2
412
41329. mv command examples
414
415Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.
416
417$ mv -i file1 file2
418
419Note: mv -f is just the opposite, which will overwrite file2 without prompting.
420
421mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument.
422
423$ mv -v file1 file2
424
42530. cat command examples
426
427You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout.
428
429$ cat file1 file2
430
431While displaying the file, following cat -n command will prepend the line number to each line of the output.
432
433$ cat -n /etc/logrotate.conf
434 1 /var/log/btmp {
435 2 missingok
436 3 monthly
437 4 create 0660 root utmp
438 5 rotate 1
439 6 }
440
44131. mount command examples
442
443To mount a file system, you should first create a directory and mount it as shown below.
444
445# mkdir /u01
446
447# mount /dev/sdb1 /u01
448
449You can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.
450
451/dev/sdb1 /u01 ext2 defaults 0 2
452
45332. chmod command examples
454
455chmod command is used to change the permissions for a file or directory.
456
457Give full access to user and group (i.e read, write and execute ) on a specific file.
458
459$ chmod ug+rwx file.txt
460
461Revoke all access for the group (i.e read, write and execute ) on a specific file.
462
463$ chmod g-rwx file.txt
464
465Apply the file permissions recursively to all the files in the sub-directories.
466
467$ chmod -R ug+rwx file.txt
468
469More chmod examples: 7 Chmod Command Examples for Beginners
47033. chown command examples
471
472chown command is used to change the owner and group of a file. \
473
474To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time.
475
476$ chown oracle:dba dbora.sh
477
478Use -R to change the ownership recursively.
479
480$ chown -R oracle:dba /home/oracle
481
48234. passwd command examples
483
484Change your password from command line using passwd. This will prompt for the old password followed by the new password.
485
486$ passwd
487
488Super user can use passwd command to reset others password. This will not prompt for current password of the user.
489
490# passwd USERNAME
491
492Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.
493
494# passwd -d USERNAME
495
49635. mkdir command examples
497
498Following example creates a directory called temp under your home directory.
499
500$ mkdir ~/temp
501
502Create nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.
503
504$ mkdir -p dir1/dir2/dir3/dir4/
505
50636. ifconfig command examples
507
508Use ifconfig command to view or configure a network interface on the Linux system.
509
510View all the interfaces along with status.
511
512$ ifconfig -a
513
514Start or stop a specific interface using up and down command as shown below.
515
516$ ifconfig eth0 up
517
518$ ifconfig eth0 down
519
520More ifconfig examples: Ifconfig: 7 Examples To Configure Network Interface
52137. uname command examples
522
523Uname command displays important information about the system such as — Kernel name, Host name, Kernel release number,
524Processor type, etc.,
525
526Sample uname output from a Ubuntu laptop is shown below.
527
528$ uname -a
529Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux
530
53138. whereis command examples
532
533When you want to find out where a specific Unix command exists (for example, where does ls command exists?), you can execute the following command.
534
535$ whereis ls
536ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
537
538When you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.
539
540$ whereis -u -B /tmp -f lsmk
541lsmk: /tmp/lsmk
542
54339. whatis command examples
544
545Whatis command displays a single line description about a command.
546
547$ whatis ls
548ls (1) - list directory contents
549
550$ whatis ifconfig
551ifconfig (8) - configure a network interface
552
55340. locate command examples
554
555Using locate command you can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb.
556
557The example below shows all files in the system that contains the word crontab in it.
558
559$ locate crontab
560/etc/anacrontab
561/etc/crontab
562/usr/bin/crontab
563/usr/share/doc/cron/examples/crontab2english.pl.gz
564/usr/share/man/man1/crontab.1.gz
565/usr/share/man/man5/anacrontab.5.gz
566/usr/share/man/man5/crontab.5.gz
567/usr/share/vim/vim72/syntax/crontab.vim
568
56941. man command examples
570
571Display the man page of a specific command.
572
573$ man crontab
574
575When a man page for a command is located under more than one section, you can view the man page for that command from a specific section as shown below.
576
577$ man SECTION-NUMBER commandname
578
579Following 8 sections are available in the man page.
580
581 General commands
582 System calls
583 C library functions
584 Special files (usually devices, those found in /dev) and drivers
585 File formats and conventions
586 Games and screensavers
587 Miscellaneous
588 System administration commands and daemons
589
590For example, when you do whatis crontab, you’ll notice that crontab has two man pages (section 1 and section 5). To view section 5 of crontab man page, do the following.
591
592$ whatis crontab
593crontab (1) - maintain crontab files for individual users (V3)
594crontab (5) - tables for driving cron
595
596$ man 5 crontab
597
59842. tail command examples
599
600Print the last 10 lines of a file by default.
601
602$ tail filename.txt
603
604Print N number of lines from the file named filename.txt
605
606$ tail -n N filename.txt
607
608View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C.
609
610$ tail -f log-file
611
612More tail examples: 3 Methods To View tail -f output of Multiple Log Files in One Terminal
61343. less command examples
614
615less is very efficient while viewing huge log files, as it doesn’t need to load the full file while opening.
616
617$ less huge-log-file.log
618
619One you open a file using less command, following two keys are very helpful.
620
621CTRL+F – forward one window
622CTRL+B – backward one window
623
624More less examples: Unix Less Command: 10 Tips for Effective Navigation
62544. su command examples
626
627Switch to a different user account using su command. Super user can switch to any other user without entering their password.
628
629$ su - USERNAME
630
631Execute a single command from a different account name. In the following example, john can execute the ls command as raj username. Once the command is executed, it will come back to john’s account.
632
633[john@dev-server]$ su - raj -c 'ls'
634
635[john@dev-server]$
636
637Login to a specified user account, and execute the specified shell instead of the default shell.
638
639$ su -s 'SHELLNAME' USERNAME
640
64145. mysql command examples
642
643mysql is probably the most widely used open source database on Linux. Even if you don’t run a mysql database on your server, you might end-up using the mysql command ( client ) to connect to a mysql database running on the remote server.
644
645To connect to a remote mysql database. This will prompt for a password.
646
647$ mysql -u root -p -h 192.168.1.2
648
649To connect to a local mysql database.
650
651$ mysql -u root -p
652
653If you want to specify the mysql root password in the command line itself, enter it immediately after -p (without any space).
65446. yum command examples
655
656To install apache using yum.
657
658$ yum install httpd
659
660To upgrade apache using yum.
661
662$ yum update httpd
663
664To uninstall/remove apache using yum.
665
666$ yum remove httpd
667
66847. rpm command examples
669
670To install apache using rpm.
671
672# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm
673
674To upgrade apache using rpm.
675
676# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm
677
678To uninstall/remove apache using rpm.
679
680# rpm -ev httpd
681
682More rpm examples: RPM Command: 15 Examples to Install, Uninstall, Upgrade, Query RPM Packages
68348. ping command examples
684
685Ping a remote host by sending only 5 packets.
686
687$ ping -c 5 gmail.com
688
689More ping examples: Ping Tutorial: 15 Effective Ping Command Examples
69049. date command examples
691
692Set the system date:
693
694# date -s "01/31/2010 23:59:53"
695
696Once you’ve changed the system date, you should syncronize the hardware clock with the system date as shown below.
697
698# hwclock –systohc
699
700# hwclock --systohc –utc
701
70250. wget command examples
703
704The quick and effective method to download software, music, video from internet is using wget command.
705
706$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
707
708Download and store it with a different name.
709
710$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701
711