Friday, 3 March 2017

50 of the most frequently used commands in the UNIX / Linux with examples



Of course, you should not take this article as a detailed guide, but it will definitely give you a starting point when working with Linux. I advise you to put yourself in the browser bookmark list.
If I missed something, let me know in the comments.

Examples of the tar command

Creating a tar archive
$ tar cvf archive_name.tar dirname/
Removing an existing archive:
$ tar xvf archive_name.tar
viewing an existing archive:
$ tar tvf archive_name.tar

Examples of grep

Insensitive search string in the file
$ grep -i "the" demo_file
The conclusion found the line, including the last 3 lines after
$ grep -A 3 -i "example" demo_text
Recursive search for a string in all files
$ grep -r "ramesh" *

Examples of the find command

Insensitive search by file name
$ find -iname "MyCProgram.c"
Executing commands on files that have been found by the find command
$ find -iname "MyCProgram.c" -exec md5sum {} \;
Search for empty files in your home directory
$ find ~ -empty

Examples SSH command

Login to the remote host
ssh -l jsmith remotehost.example.com
Debugging ssh client
ssh -v -l jsmith remotehost.example.com
Conclusion ssh client version
$ ssh -V

OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003

Examples sed command

When copying a DOS file to UNIX, you can often find \ r \ n at the end of each line. This example translates all DOS names to Unix.
$ sed 's/.$//' filename
Printing the file contents in the reverse order
$ sed -n '1!G;h;$p' thegeekstuff.txt
Adding line numbers to all non-empty strings
$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'

Examples of the awk command

Removing duplicate rows
$ awk '!($0 in array) { array[$0]; print }' temp
Displays all rows from /etc/passwda repetitive uidandgid
$awk -F ':' '$3==$4' passwd.txt
Print specific fields from file
$ awk '{print $2,$5;}' employee.txt

Examples of vim commands

Go to the 143 line of the file
$ vim +143 filename.txt
The transition to the found line
$ vim +/search-term filename.txt
Opening a file in read-only mode
$ vim -R /etc/passwd

Examples of the diff command

Skipping blanks during comparison
$ diff -w name_list.txt name_list_new.txt
  1. examples of the sort command

Sort files in ascending order
$ sort names.txt
Sorting in descending order of the files
$ sort -r names.txt
Sort passwd file contents of the third field
$ sort -t: -k 3n /etc/passwd | more

Examples export command

Output variables related to oracle
$ export | grep ORACLE

declare -x ORACLE_BASE="/u01/app/oracle"
declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
declare -x ORACLE_SID="med"
declare -x ORACLE_TERM="xterm"
Export environment variables
$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0

Examples xargs command

Copying all the images on an external drive
$ ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
Search all images in jpg and archiving system
$ find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
Zagruzuka all the url of the file url-list.txt
$ cat url-list.txt | xargs wget –c

Examples of the ls command

The output file size in a readable format
$ ls -lh
-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz
Sort by last date modified files (in reverse order)
$ ls -ltr
Visual classification of files with special characters
$ ls -F

The pwd command

The command pwddisplays the current working directory. More here and I have nothing to add.

Examples of the cd command

Use cd -to move to the previous directory is used shopt -s cdspellto automatically correct typos

Examples gzip command

Create *.gzarchive
$ gzip test.txt
Extract the archive *.gz:
$ gzip -d test.txt.gz
Output compression ratio in the archives
$ gzip -l *.gz
        compressed        uncompressed  ratio uncompressed_name
             23709               97975  75.8% asp-patch-rpms.txt

Examples bzip2 command

Creating an archive *.bz2:
$ bzip2 test.txt
Extract the archive *.bz2:
bzip2 -d test.txt.bz2

Examples unzip command

Creating an archive *.zip:
$ unzip test.zip
Displays contents of an archive *.zip, without extracting:
$ unzip -l jasper.zip

Archive:  jasper.zip
 Length     Date   Time    Name
--------    ----   ----    ----
   40995  11-30-98 23:50   META-INF/MANIFEST.MF
   32169  08-25-98 21:07   classes_
   15964  08-25-98 21:07   classes_names
   10542  08-25-98 21:07   classes_ncomp

Examples shutdown command

Ostanalivaet system and turn off power
$ shutdown -h now
Turn off the system after 10 minutes
$ shutdown -h +10
Reloading
$ shutdown -r now
Check file system when rebooting
$ shutdown -Fr now

Examples ftp command

To connect to a remote server and download multiple files, use the following command:
$ ftp IP/hostname
ftp> mget *.html
To view the files on the server before you download them, use:
ftp> mls *.html -
/ftptest/features.html
/ftptest/index.html
/ftptest/othertools.html
/ftptest/samplereport.html
/ftptest/usage.html

Examples crontab command

Conclusion crowns user tasks
$ crontab -u john -l
Running CZK task every 10 minutes
*/10 * * * * /home/ramesh/check-disk-space

Examples of service commands

The command serviceis used to run the system scripts, instead of what would have to run them with the full path of the /etc/init.d.
Checking the status of the service:
$ service ssh status
Checking the status of all services:
service --status-all
Restarting the service:
$ service ssh restart

Examples of the ps command

Teams psdisplays information about the processes running on the system. The team pstakes a huge amount of arguments, but the most common ones are as follows:
Output current running processes:
$ ps -ef | more
Conclusion processes as a tree:
$ ps -efH | more

Examples of free command

This command is used for the conclusion of free, used memory and memory paging file on the system.
The output. Values ​​in bytes.
$ free
            total       used       free     shared    buffers     cached
Mem:       3566408    1580220    1986188          0     203988     902960
-/+ buffers/cache:     473272    3093136
Swap:      4000176          0    4000176
If you want to see how many gigs of memory left in the system, use the -g switch. -k - the output in kilobytes, -m - in megabytes
$ free -g
            total       used       free     shared    buffers     cached
Mem:             3          1          1          0          0          0
-/+ buffers/cache:          0          2
Swap:            3          0          3
Use the key tto calculate the resulting value of memory:
ramesh@ramesh-laptop:~$ free -t

            total       used       free     shared    buffers     cached
Mem:       3566408    1592148    1974260          0     204260     912556
-/+ buffers/cache:     475332    3091076
Swap:      4000176          0    4000176
Total:     7566584    1592148    5974436

Examples of top teams

It displays the most demanding processes in the system (the default is sorted by using cpu). To change the sort column using O.
Current Sort Field:  P  for window 1:Def
Select sort field via field letter, type any other key to return

 a: PID        = Process Id              v: nDRT       = Dirty Pages count
 d: UID        = User Id                 y: WCHAN      = Sleeping in Function
 e: USER       = User Name               z: Flags      = Task Flags
 ........
To display processes of one user using u-key. The following command displays the processes owned by the user oracle.
$ top -u oracle

Examples of df command

Displays the number of additional disk space. By default, in bytes (-k)
$ df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             29530400   3233104  24797232  12% /
/dev/sda2            120367992  50171596  64082060  44% /home
df -h displays the size in gigabytes
ramesh@ramesh-laptop:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              29G  3.1G   24G  12% /
/dev/sda2             115G   48G   62G  44% /home
-T Use the key to display the type of system
ramesh@ramesh-laptop:~$ df -T
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/sda1     ext4    29530400   3233120  24797216  12% /
/dev/sda2     ext4   120367992  50171596  64082060  44% /home

Examples of the kill command

It is used to complete the process. First, determine the process id command ps -ef, and then use the kill -9, you will be able to complete the process. Also for this purpose may be used killallpkillxkill.
$ ps -ef | grep vim

ramesh    7243  7222  9 22:43 pts/2    00:00:00 vim
$ kill -9 7243

Examples of the rm command

Deleting a file confirmation
$ rm -i filename.txt
Pretty useful to use wildcards in the file name in the argument of the command output file name and a request to confirm the deletion:
$ rm -i file*
In the following example, recursively delete all files from the example directory, including the directory itself
$ rm -r example

Examples of the cp command

Kopirovanit of file1 to file2 preserving mode, ownership and timestamps.
$ cp -p file1 file2
Copy file1 to file2. If file2 already exists, you are prompted for permission to overwrite
$ cp -i file1 file2

Examples of the mv command

Rename file1 to file2. If file2 exists, you are prompted to overwrite permission.
$ mv -i file1 file2
Note mv -f overwrites the default file, without query.
mv -v it displays what is happening in the process of renaming, which is especially useful when using meta-characters in the team.
$ mv -v file1 file2

Examples of the cat command

Displays the contents of several files. The following example displays the contents of file1 and file2 to stdout.
$ cat file1 file2
The -n option to add a line number in the output:
$ cat -n /etc/logrotate.conf
   1        /var/log/btmp {
   2            missingok
   3            monthly
   4            create 0660 root utmp
   5            rotate 1
   6        }

Examples of the mount command

To mount the file system, you first need to create a directory and only then mount the system in it.
$ mkdir /u01
$ mount /dev/sdb1 /u01
You can also add the following line to the fstab file, and these file systems will be mounted automatically when the system boots. / Dev / sdb1 / u01 ext2 defaults 0 2
32. примеры команды chmod
Used to change the file or directory rights. We provide full rights user and group:
$ chmod ug+rwx file.txt
Cancellation rights issued:
$ chmod g-rwx file.txt

The use rights recursively all directories and files:
$ chmod -R ug+rwx file.txt

Examples chown command

The command is used to change the file or directory owner. To change the owner to oracle, a group on the db, should change both parameters simultaneously:
$ chown oracle:dba dbora.sh
Use the -R option to apply the command recursively:
$ chown -R oracle:dba /home/oracle

Examples of the passwd command

Password changes in the user terminal. You will be prompted to enter your old and new password:
$ passwd
Super users can change passwords of other users. The request for the old password will not be displayed.
# passwd USERNAME
Deleting a user password. Root users can disable the password for any user. Once the password is removed, the user will be able to log in without a password.
# passwd -d USERNAME

Examples of the mkdir command

Create a temp directory in your home directory
$ mkdir ~/temp
Creating a sub-directory with a single command. If one of the directory already exists, no error occurs. If a directory is not available, it will be created.
$ mkdir -p dir1/dir2/dir3/dir4/

Examples of ifconfig

Command to display the current network interface settings and change them. The output interface list with the current settings.
$ ifconfig -a
Starting and stopping a particular interface.
$ ifconfig eth0 up
$ ifconfig eth0 down

Examples uname command

Displays important information about the system, such as the kernel name, and the host kernel version, CPU type, and so on.
command output example:
$ uname -a

Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux

Examples whereis command

It displays the location of a specific command linux:
$ whereis ls

ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
If you want to search beyond the tracks, which are incorporated in the whereis command, you can use the -B key to adding new routes. The following command searches for the executable file in the tmp directory and removes it, if it was found:
$ whereis -u -B /tmp -f lsmk
lsmk: /tmp/lsmk

Examples whatis command

Displays a one-line description of the command.
$ whatis ls
ls                (1)  - list directory contents

$ whatis ifconfig
ifconfig (8)         - configure a network interface

Examples of the locate command

It gives the location of a specific file (or groups of files). The team uses a database created by updatedb command.
The following command lists the files in which the name of a line crontab present.
$ locate crontab

/etc/anacrontab
/etc/crontab
/usr/bin/crontab
/usr/share/doc/cron/examples/crontab2english.pl.gz
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/vim/vim72/syntax/crontab.vim

Examples of the man command

Command displays instructions to the team.
$ man crontab
If there are several instructions to the same team, then select the desired, you can specify a particular section:
$ man SECTION-NUMBER commandname
In total there are 8 sections.
  • Basic commands
  • System calls
  • C library functions
  • Special files (usually in / dev) and driver
  • File formats and conventions
  • Games and screensavers
  • Miscellaneous
  • System commands and daemons
For example, if you turn to page crontab command documentation, you will find it in two sections (1 and 5). To print an article from section 5, do the following:
$ whatis crontab

crontab (1)          - maintain crontab files for individual users (V3)
crontab (5)          - tables for driving cron
$ man 5 crontab

Examples tail command

By default displays the last 10 lines of the file.
$ tail filename.txt
Output N lines from the filename.txt file
$ tail -n N filename.txt
Display contents of a file in real-time using the tail -f. This approach is very useful when viewing log files. Stop command can be CTRL-C combination.
$ tail -f log-file

Examples of the less command

This command is very useful for viewing large log files, as it does not load the entire file.
$ less huge-log-file.log
Use the following two commands to navigate through the open file:
CTRL + F - forward CTRL + B box - back to the window

Examples of the su command

Moving to another user account. Super user can go to any other account without entering a password.
$ su - USERNAME
Executing a single command as a different user. In the following example, john executes ls command as the user raj. After the command, control passes to the user john.
[john@dev-server]$ su - raj -c 'ls'
[john@dev-server]$
Going by another user, and implementation of the a shell instead of this default.
$ su -s 'SHELLNAME' USERNAME

Examples mysql command

Mysql, perhaps, ranks first in the number of databases with open source. Even if your server is not running and the server mysql, mysql client that you are probably using too early to access the database on a remote server.
The command connects you to the remote server and requests a password.
$ mysql -u root -p -h 192.168.1.2
The connection to the local database.
$ mysql -u root -p
If you want to enter a password to connect directly to the team, then enter it immediately after the -p (without spaces) key.

Examples yum command

Installing apache using yum.
$ yum install httpd
Update apache using yum.
$ yum update httpd
Removing apache using yum.
$ yum remove httpd

Examples of the rpm command

Installing apache using rpm.
# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm
Update apache using rpm.
$ rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm
Removing apache using rpm.
$ rpm -ev httpd

Examples of the ping command

Ping five packets to the remote server.
$ ping -c 5 gmail.com
  1. examples of the date command
Set the system date:
$ date -s "01/31/2010 23:59:53"
After you change the system date to synchronize hardware clock:
$ hwclock –systohc
# hwclock --systohc –utc

Examples wget command

A quick way to load resources from the network:
$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
Loading and saving under a different name.
$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701
I missed something? Leave a comment and share your examples.

1 comment:

  1. it is a little unlikely ,draw up a list of the most frequently used commands from the terminal, unix-linux, since each of us has a use of a different computer . Some of the commands mentioned above ,I do not use them ever ,much less use vim.. (is just an example), anyway, nice article to keep in your favorites . A few years ago I used to save and print this type of articles . LOL.

    ReplyDelete