50 Linux Basic Operations Commands Must Be Mastered

Explain:

Based on the author's usage in peacetime and some other reference materials, the following 50 basic Linux commands that must be mastered are carefully sorted out.

Command:

1. ssh

Log on to remote host

$ ssh -l jsmith remotehost.example.com

Debugging ssh client

$ ssh -v -l jsmith remotehost.example.com

Show ssh client version

$ ssh -V

2. sed

When you copy a file from your Dos system to Unix/Linux, each line of the file ends with \r\n. sed can easily convert it to a file in Unix format and use the file that ends with \n

$ sed 's/.$//' filename

Reverse file contents and output

$ sed -n '1!G; h; p' filename

Add line numbers for non-empty lines

$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'

3. tar

Create a new tar file

$ tar cvf archive_name.tar dirname/

Unzip tar file

$ tar xvf archive_name.tar

View tar file

$ tar tvf archive_name.tar

4. grep

Find strings in files (case insensitive)

$ grep -i "the" demo_file

Output rows that match successfully, and three rows after that row

$ grep -A 3 -i "example" demo_text

Recursively queries a folder for files containing the specified string

$ grep -r "ramesh" *

5. find

Find the file with the specified file name (case insensitive)

$ find -iname "MyProgram.c"

Execute a command on the file found

$ find -iname "MyProgram.c" -exec md5sum {} \;

Find all empty files in the home directory

$ find ~ -empty

6. awk

Delete duplicate rows

$ awk '!($0 in array) { array[$0]; print}' temp

Print/etc/passwd all lines containing the same uid and gid

$ awk -F ':' '$3=$4' /etc/passwd

Print the fields of the specified part of the file

$ awk '{print $2,$5;}' employee.txt

7. vim

Open the file and jump to line 10

$ vim +10 filename.txt

Open the file and jump to the first matching line

$ vim +/search-term filename.txt

Open file in read-only mode

$ vim -R /etc/passwd

8. diff

Ignore whitespace when comparing

$ diff -w name_list.txt name_list_new.txt

9. sort

Sort file contents in ascending order

$ sort names.txt

Sort file contents in descending order

$ sort -r names.txt

Sort the contents of /etc/passwd by the third field

$ sort -t: -k 3n /etc/passwd | more

10. export

Output environment variable matching string oracle

$ export | grep ORCALE
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"

Setting global environment variables

$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0

11. xargs

Copy all picture files to an external drive

$ ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

Compress and package all jpd files in the system

$ find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

Download pages corresponding to all URLs listed in the file

$ cat url-list.txt | xargs wget –c

12. ls

Display file size in an easy-to-read way (MB,GB...)

$ ls -lh-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz

Output file in ascending sequence with last modification

$ ls -ltr

Show file type after file name

$ ls -F

13. pwd

Output Current Working Directory

14. cd

cd - can switch between two recently working directories

Use shopt-s cdspell to set up automatic spell checking for cd commands

15. gzip

Create a *.gz compressed file

$ gzip test.txt

Unzip *.gz file

$ gzip -d test.txt.gz

Show compression ratio

$ gzip -l *.gz

16. bzip2

Create *.bz2 compressed file

$ bzip2 test.txt

Unzip *.bz2 file

bzip2 -d test.txt.bz2

17. uzip

Unzip*.zip file

$ unzip test.zip

View the contents of the *.zip file

$ unzip -l jasper.zipArchive: jasper.zipLength 

18. shutdown

Shut down the system and shut down immediately

$ shutdown -h now

Shut down after 10 minutes

$ shutdown -h +10

restart

$ shutdown -r now

Force system checks during restart

$ shutdown -Fr now

19. ftp

The usage of the ftp and sftp commands is essentially similar. Connect to the ftp server and download multiple files

$ ftp IP/hostname

ftp> mget *.html

Displays a list of files on a remote host

ftp> mls *.html -/ftptest/features.html/ftptest/index.html/ftptest/othertools.html/ftptest/samplereport.html/ftptest/usage.html

20. crontab

View a user's crontab entry

$ crontab -u john -l

Set up a scheduled task to be performed every ten minutes

*/10 * * * * /home/ramesh/check-disk-space

21. service

The service command is used to run System V init scripts, which are usually located under the / etc/init.d file. This command can run scripts directly inside this folder without adding a path

View service status

$ service ssh status

View all service status

$ service --status-all

Restart Service

$ service ssh restart

22. ps

The ps command displays information about a running process

View all currently running processes

$ ps -ef | more

Show currently running processes in a tree structure, and the H option shows the hierarchy of processes

$ ps -efH | more

23. free

This command displays the current memory usage of the system, including used, available, and swapped memory

free outputs memory usage in bytes by default

$ free

If you want to output memory usage in other units, you need to add an option, -g for GB, -m for MB, -k for KB, and-b for bytes.

$ free -g

If you want to see a summary of all memory, use the -t option, which adds a summary line to the output

$ free -t

24. top

The top command displays some of the most resource-intensive processes in the current system (sorted by CPU occupancy by default). If you want to change the sort order, you can click O (uppercase O) in the result list to display all the columns that can be sorted. At that time, you can select the columns you want to sort.

Current Sort Field: P for window 1:DefSelect 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

........

You can use the -u option if you only want to show processes for a specific user

$ top -u oracle

25. df

Displays disk usage for the file system. By default, df-k outputs disk usage in bytes

$ df -kFilesystem 1K-blocks Used Available Use% Mounted on/dev/sda1 29530400 3233104 24797232 12% //dev/sda2 120367992 50171596 64082060 44% /home

Use the -h option to display disk usage in a more readable way

$ df -h

Show file system type with -T option

$ df -T

26. kill

Kill is used to terminate a process.Generally, we use ps-ef to find a process and get its process number, then kill-9 process number to terminate the process.You can also kill all, pkill, xkill to terminate the process

$ ps -ef | grep vim

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

$ kill -9 7243

27. rm

Confirm before deleting files

$ rm -i filename.txt

It is useful to use shell metacharacters in file names.Print and confirm file name before deleting file

$ rm -i file*

Recursively delete all files under a folder and delete the folder

$ rm -r example

28. cp

Copy Files 1 to 2, and keep file permissions, ownership, and timestamp

$ cp -p file1 file2

Copy file1 to file2, if File2 exists it will be prompted to overwrite

$ cp -i file1 file2

29. mv

Rename file name file1 to file2 and prompt for overwriting if file2 exists

$ mv -i file1 file2

Note that you will not be prompted if the -f option is used

-v outputs the rename process, which is convenient when the file name contains wildcards

$ mv -v file1 file2

30. cat

You can view the contents of multiple files at once. The following command will print the contents of file1 and then the contents of file2

$ cat file1 file2

-n command can precede each line with a line number

$ cat -n /etc/logrotate.conf/var/log/btmp {

missingok3     monthly4     create 0660 root utmp5     rotate 16 }

31. mount

If you want to mount a file system, you need to create a directory and then mount the file system to it

# mkdir /u01

# mount /dev/sdb1 /u01

It can also be added to fstab for automatic mounting so that the file system will be loaded whenever the system restarts

/dev/sdb1 /u01 ext2 defaults 0 2

32. chmod

chmod is used to change permissions for files and directories

Give all permissions (including read, write, execute) to the owner and group of the specified file

$ chmod ug+rwx file.txt

Delete all permissions of the group belonging to the specified file

$ chmod g-rwx file.txt

Permissions to modify directories and to recursively modify all files and subdirectories under directories

$ chmod -R ug+rwx file.txt

33. chown

chown is used to change file ownership and group

Also change the owner of a file to oracle and the owner group to db

$ chown oracle:dba dbora.sh

Recursive modification of directories and files under directories using the -R option

$ chown -R oracle:dba /home/oracle

34. passwd

passwd is used to change the password on the command line, which requires you to enter the old password before entering the new one

$ passwd

Superuser can use this command to change other user's password without entering the user's password at this time

# passwd USERNAM

passwd can also delete a user's password. This command can only be operated by the root user. After deleting the password, the user can log on to the system without entering the password.

# passwd -d USERNAME

35. mkdir

Create a directory named temp in the home directory

$ mkdir ~/temp

Use the -p option to create a directory that does not exist on the path

$ mkdir -p dir1/dir2/dir3/dir4/

36. ifconfig

ifconfig is used to view and configure network interfaces for Linux systems

View all network interfaces and their status

$ ifconfig -a

Start or stop an interface using up and down commands

$ ifconfig eth0 up
$ ifconfig eth0 down

37. uname

uname can display important system information such as kernel name, host name, kernel version number, processor type, etc.

$ uname -a
Linux localhost.localdomain 4.18.16-200.fc28.x86_64 #1 SMP Sat Oct 20 23:53:47 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

38. whereis

Use the whereis command when you don't know the location of a command. Use whereis below to find the location of ls

$ whereis ls

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

When you want to find the location of an executable program that is not in the default directory of whereis, you can use the -B option and specify the directory as a parameter of this option.The following command finds the lsmk command in the / tmp directory

$ whereis -u -B /tmp -f lsmk

lsmk: /tmp/lsmk

39. whatis

wathis displays a description of a command

$ whatis ls

ls        (1) - list directory contents

$ whatis ifconfig

ifconfig (8) - configure a network interface

40. locate

Loce naming shows the path to a specified file (or set of files) that uses a database created by updatedb

The following command displays all the files in the system that contain the crontab string

$ locate

41. man

man page showing a command

$ man crontab

Some commands may have multiple man pages, each of which corresponds to a command type

$ man SECTION-NUMBER commandname

The man page can generally be divided into eight command types

1. User Commands
2. System Calls
3.c Library Functions
4. Device and Network Interface
5. File format
6. Game and Screen Saver
7. Environment, Table, Macro
8. System Administrator Commands and Background Run Commands

For example, if we execute whatis crontab, you can see that crontab has two command types 1 and 5, so we can view the man page of command type 5 with the following command

$ whatis crontab

crontab (1) - maintain crontab files for individual users (V3)

crontab (5) - tables for driving cron

$ man 5 crontab

42. tail

The tail command defaults to the last 10 lines of text in the file

$ tail filename.txt

You can use the -n option to specify the number of rows to display

$ tail -n N filename.txt

You can also use the -f option for real-time viewing. This command waits after execution, and if a new line is added to the end of the file, it continues to output a new line, which is useful when viewing the log.You can terminate command execution via CTRL-C

$ tail -f log-file

43. less

This name can display the contents of a file without loading the entire file, which is useful when viewing large log files

$ less huge-log-file.log

When you open a file with the less command, the next two keys will help you a lot by scrolling the screen forward and backward

CTRL+F – forward one window

CTRL+B – backward one window

44. su

The su command is used to switch user accounts, and the superuser can switch to any other user without entering a password.

$ su - USERNAME

Execute a command with another username In the following example, user Jack executes ls command with test username and returns Jack's account after execution

[Jack@dev-server]$ su - test -c 'ls'

Jack@dev-server]$

Log in with the specified user and use the specified shell program instead of the default

$ su -s 'SHELLNAME' USERNAME

45. mysql

mysql is probably the most widely used database on Linux, and even if you do not have mysql installed on your server, you can use the mysql client to connect to a remote mysql server

Connecting to a remote database requires a password

$ mysql -u root -p -h 192.168.1.2

Connect to local database

$ mysql -u root -p

You can also enter the database password from the command line, simply append the password after -p as a parameter, and write it directly after P without adding spaces

46. yum

Install apache using yum

$ yum install httpd

Update apache

$ yum update httpd

Uninstall/Remove apache

$ yum remove httpd

47. rpm

Install apache using rpm

# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm

Update apache

# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm

Uninstall/Remove apache

# rpm -ev httpd

48. ping

ping a remote host and send only 5 packets

$ ping -c 5 www.baidu.com

49. date

Set System Date

# date -s "01/31/2010 23:59:53"

When you modify the system time, you need to synchronize the hardware time with the system time

# hwclock –systohc

# hwclock --systohc –utc

50. wget

Download version 3.62 of Python from the web using wget

$ wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz

Download the file and save it with the specified file name

$ wget -O Python.tgz https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz

Tags: Linux crontab Oracle Python ftp

Posted on Mon, 03 Feb 2020 11:43:59 -0500 by mgason