All linux commands with options

40 Basic Linux Commands used Frequently

In this tutorial, I will show the very basic Linux commands with examples that are frequently used to get you more familiar with the Linux command line. To be an expert in Linux first step for a beginner would be to start learning the basic commands.

The command is followed by options (optional of course) and a list of arguments. The options can modify the behavior of a command. The arguments may be files or directories or some other data on which the command acts. Every command might not need arguments. Some commands work with or without them (e.g. ‘ls’ command). The options can be provided in two ways: full word options with — (e.g. —help), or single letter options with — (e.g. -a -b -c or multiple options, -abc).

Syntax

The commands in Linux have the following syntax:

Linux Basic Commands

Let’s start with some simple commands.

1) pwd command

‘pwd’ command prints the absolute path to current working directory.

2) cal command

Displays the calendar of the current month.

‘cal ’ will display calendar for the specified month and year.

3) echo command

This command will echo whatever you provide it.

The ‘echo’ command is used to display the values of a variable. One such variable is ‘HOME’. To check the value of a variable precede the variable with a $ sign.

4) date command

Displays current time and date.

If you are interested only in time, you can use ‘date +%T’ (in hh:mm:ss):

5) tty command

Displays current terminal.

6) whoami command

This command reveals the user who is currently logged in.

7) id command

This command prints user and groups (UID and GID) of the current user.

By default, information about the current user is displayed. If another username is provided as an argument, information about that user will be printed:

8) clear command

This command clears the screen.

Help command

Nobody can remember all the commands. We can use help option from command like

9) help option

With almost every command, ‘—help’ option shows usage summary for that command.

10) whatis command

This command gives a one line description about the command. It can be used as a quick reference for any command.

11) Manual Pages

‘—help’ option and ‘whatis’ command do not provide thorough information about the command. For more detailed information, Linux provides man pages and info pages. To see a command’s manual page, man command is used.

The man pages are properly documented pages. They have following sections:

NAME: The name and one line description of the command.

SYNOPSIS: The command syntax.

DESCRIPTION: Detailed description about what a command does.

OPTIONS: A list and description of all of the command’s options.

EXAMPLES: Examples of command usage.

FILES: Any file associated with the command.

AUTHOR: Author of the man page

REPORTING BUGS: Link of website or mail-id where you can report any bug.

SEE ALSO: Any commands related to the command, for further reference.

With -k option, a search through man pages can be performed. This searches for a pattern in the name and short description of a man page.

12) Info pages

Info documents are sometimes more elaborate than the man pages. But for some commands, info pages are just the same as man pages. These are like web pages. Internal links are present within the info pages. These links are called nodes. Info pages can be navigated from one page to another through these nodes.

Linux Filesystem commands

13) Changing Directories Command

Change the current working directory to the directory provided as argument. If no argument is given to ‘cd’, it changes the directory to the user’s home directory. The directory path can be an absolute path or relative to current directory. The absolute path always starts with /. The current directory can be checked with ‘pwd’ command (remember?):

In the first ‘cd’ command, absolute path (/usr/share) is used, and with second command, relative path (doc) is used.

14) Listing File And Directories Command

List files and/or directories. If no argument is given, the contents of current directory are shown.

If a directory is given as an argument, files and directories in that directory are shown.

‘ls -l’ displays a long listing of the files.

In this long listing, the first character is ‘d’ or ‘-‘. It distinguishes between file types. The entries with a ‘-‘ (dash) are regular files, and ones with ‘d’ are directories. The next 9 characters are permissions (‘rwxr-xr-x’ in first listing). The number following the permissions is the link count. Link count follows user and group owner. In the above example, the file owner is ‘raghu’ and group owner is ‘raghu’ as well. Next is the size of the file. And then time stamp before the name of file (or directory).

By default, hidden files or directories are not shown, to see hidden files as well, -a option is used. Hidden files in Linux start with a period sign (.). Any file that starts with a period is hidden. So, to hide a file, you just need to rename it (and put a period before it).

If you want to see the properties of a directory instead of the files contained in it, use -d (with -l) option:

Creating files and directories

15) mkdir command

To create a directory, the ‘mkdir’ command is used.

16) touch command

For creating an empty file, use the touch command.

If a file already exists, touch will update its time stamp. There are a lot of other methods to create a new file, e.g. using a text editor like vi or gedit, or using redirection. Here is an example of creating a file using redirection:

A file named usrlisting is created in this example.

Copy, move and remove commands

17) copy command

Copy files and directories. If the source is a file, and the destination (file) name does not exit, then source is copied with new name i.e. with the name provided as the destination.

If the destination is a directory, then the file is copied with its original name in that directory.

Multiple files can also be copied, but in that case, the last argument will be expected to be a directory where all the files are to be copied. And the rest of the arguments will be treated as file names.

If a directory is to be copied, then it must be copied recursively with the files contained in it. To copy a directory recursively, use -r option with ‘cp’ command:

18) move command

Move files or directories. The ‘mv’ command works like ‘cp’ command, except that the original file is removed. But, the mv command can be used to rename the files (or directories).

Here, ‘listing_copy.txt’ is moved with the name ‘usrcopy’ in the same directory (or you can say that it has been renamed).

19) To remove or Delete

‘rmdir’ command removes any empty directories, but cannot delete a directory if a file is present in it. To use ‘rmdir’ command, you must first remove all the files present in the directory you wish to remove (and possibly directories if any).

To remove files and directories

A directory must be removed recursively with -r option.

Here, the file named ‘file2’ is removed first, and then the directory ‘example’ is removed recursively. This can be seen in the output of ‘ls -l’ command where these two are no longer present.

Читайте также:  Загрузка возобновления windows последняя попытка

Other file commands

20) file command

The file command determines the file type of a given file. For example:

You can provide one or more than one file as an argument to the file command.

21) stat command

To check the status of a file. This provides more detailed information about a file than ‘ls -l’ output.

22) cat command

The ‘cat’ command is actually a concatenator but can be used to view the contents of a file.

23) pagers

The cat command lists file as a whole. But if the file is big enough to fit into one screen, then we will be able to see only the last page of the file. The commands ‘less’ and ‘more’ display files one page at a time. So they are also called pagers. You can navigate through a file using arrow keys. To quit from a pager, hit ‘q’.

24) head command

Displays the first few lines of a file. By default, the ‘head’ command displays the first 10 lines of a file. But with -n option, the number of lines to be viewed can be specified.

25) tail command

Similar to ‘head’; the ‘tail’ command shows the last 10 lines by default, and -n option is available as well.

26) wc command

This command counts lines, words and letters of the input given to it.

The /etc/passwd file has 35 lines, 57 words, and 1698 letters present in it.

27) grep command

The ‘grep’ command searches for a pattern in a file (or standard input). It supports regular expressions. It returns a line if it matches the pattern in that line. So, if we wish to find the lines containing the word ‘nologin’, we use ‘grep’ as follows:

28) ln command

The ln command is used in linux to create links. Links are a kind of shortcuts to other files. The general form of command is:

There are two types of links, soft links and hard links. By default, hard links are created. If you want to create soft link, use -s option. In this example, both types of links are created for the file usrlisting.

Text Editors

29) Pico & Nano

‘Pico’ is a text editor in Linux. ‘Nano’ editor is inspired from ‘pico’. They work almost the same. If the argument given as filename exists, then that file will be opened for editing in pico/nano. Otherwise, a new file with that name will be created. Let’s create a new file named hello.txt:

Having made all the changes to the file, press ‘ctrl+o’ to write the changes to the file and ‘ctrl+x’ to exit from the editor. There are a lot of functions available with this editor. The help menu can be accessed with ‘ctrl+g’ keystrokes.

30) VI editor

The VI stands for Visual editor; another text editor in Linux. This is a standard editor in many Linux/Unix environments. This is the default editor that comes with many Linux distributions. It might be possible that it is the only text editor available with your distro.

You can open a file with vi for editing using the following:

The vi editor has 3 modes in which it performs its functions. The default is COMMAND mode, in which tasks like copy, paste, undo etc can be performed. You can change a mode from command mode only (and come back to it). The second mode is the INSERT mode, in which whatever key you type is treated as a character and will be loaded into the file buffer. To enter this mode, press ‘i’ when in command mode.
The final mode is EX mode or last line mode. The changes made in the buffer can be saved or discarded in this mode.

Useful commands

31) alias command

The ‘alias’ is another name for a command. If no argument is given, it shows current aliases. Aliases can be used for short names of commands. For example, you might use the clear command frequently. You can create an alias for it:

Next time you enter ‘c ‘ on command line, your screen will get clear. Current aliases can be checked with ‘alias’ command:

32) w command

w command is used to check which users are logged in to the system, and what command they are executing at that particular time:

It also shows the uptime, number of users logged in and load average of the system (in the first line of output above).

33) last command

Displays information about the users who logged in and out of the system. The output of the last command can be very large, so the following output has been filtered (through head) to display the top 10 lines only:

A similar command is ‘lastb’ that shows the last unsuccessful login attempts. But this command must be run as root otherwise you would get an error saying permission denied.

34) du command

The du command determines disk usage of a file. If the argument given to it is a directory, then it will list disk usage of all the files and directories recursively under that directory:

35) df command

The df reports file system usage. For example:

36) fdisk command

The fdisk is a tool for getting partition information, and for adding and removing partitions. The fdisk tool requires super user privileges. To list all the partitions of all the hard drives available:

The fdisk is an interactive tool to edit the partition table. It takes a device (hard disk) as an argument, whose partition table needs to be edited.

Pressing ‘m’ at the fdisk prompt prints out the help shown above that lists all the commands available for fdisk. A new partition can be created with ‘n’ and an existing partition can be deleted with the ‘d’ command. When you are done editing the partitions, press ‘w’ to write the changes to the disk, and finally, hit ‘q’ to quit from fdisk (q does not save changes).

37) netstat command

The ‘netstat’ is a command used to check the network statistics of the system. It will list the current network connections, routing table information, interface statistics, masquerade connections and a lot more information.

38) history command

History command shows the commands you have entered on your terminal so far.

39) passwd command

To change your password with passwd command.

40) Shutdown Command

In Linux, you can use shutdown command to gracefully halt your system. Most commonly used command is shutdown -h now .

Источник

Linux Commands — Complete Guide

If you are a beginner in Linux you might have started using the command line. Once familiarized it is fairly easy to work from the terminal and that why most Linux sysadmins prefer over the GUI.

This tutorial we did best efforts to add most frequently used and important Linux commands you should know and remember. If you are looking for Linux cheat sheet we have in a downloaded format.

System related Linux commands

These commands are used to view and manage Linux system-related information.

1. uname

Uname command is used in Linux to find operating systems information. Uname had many options that can provide kernel name, kernel version, CPU type and hostname.

The following uname command with a option displays all information about the operating system.

2. uptime

Information about how long the Linux system has been running is provided using uptime command. System uptime information is gathered from ‘/proc/uptime’ file. This command will also display the system load average.

From the following command we can understand that the system is up and running for the last 13 mins.

3. hostname

You can display your machine hostname by typing hostname in your terminal. With -i option you can view the ip address of the machine and with -d you can view the domain name.

4. last

Linux’s last command is used to see who is the last logged in user into your server. Last command displays a list of all user logged in (and out) from ‘/var/log/wtmp’ since the file was created.

You just need to type ‘last’ on your terminal. Here’s is an example

Читайте также:  Hp elitebook не устанавливает windows

5. date

In Linux, date command is used to check the current date and time of the system. This command allows you to set custom formats for dates.

As an example, by using ‘date +%D’ you can view the date in ‘MM/DD/YY’ format.

6. cal

By default, cal command display the calendar of the current month. With -y option you can view the calendar of the whole year.

7. w

By default, w command displays who is currently logged in into your machine and what they are doing.

8. whoami

Whoami command prints the user name of the currently logged in user. It is similar to id -un command.

9. reboot

To immediately reboot a linux system you can run this command from terminal with root privilege.

10. shutdown

Shutdown command is used to halt or reboot a Linux system. This command has options to schedule shutdown and notify users with custom messages.

By default, the machine will go down in 1 minute and you can cancel the schedule by issuing shutdown -c

The system can be shut down immediately by providing ‘now’ option.

Hardware Related Linux Commands

These commands are used to view and manage hardware-related aspects of the Linux machine.

Dmesg command prints its data by reading the kernel ring buffer. The data can be information about the processor, hard disk, printer, keyboard, memory and drivers.

Displays information on the hardware configuration of the system. Lshw command must be run as root or sudo privileged user otherwise it will only report partial information.

Displays block device-related information of the machine. Lsblk command with -a you can view all block devices.

Free command is used in Linux to check the amount of free RAM memory in the system. The free command also shows swap space, buffer and cache space.

15. lspci

Lspci is a standard Linux utility that shows what your system’s hardware has got internally. You can expect your results to have information about PCI buses, AGP and onboard components like USB chipset etc.

16. lsusb

Lsusb command is used to display information about USB buses in the system and the devices connected to them You can find lsusb utility in usbutils package.

Dmidecode command reads the system DMI (Desktop Management Interface) table to display hardware and BIOS information of the server. The output of the dmidecode contains several records from the DMI table. This table contains a description of the systems hardware components and also other useful information such as serial number, BIOS revision, etc

You need root or sudo privilege to run this command. The following examples show the bios details.

18. hdparm

Hdparm is a command used in Linux print hard disk information. It can perform disk speed performance and change DMA settings.

The following command will perform a read speed test on disk sda:

Statistics Related Linux Commands

These set of commands are used to view various kinds of stats of the Linux system.

19. mpstat

Mpstat command is used to print processor statistics and helps to monitor CPU utilization on your system. It will be more useful if your system has multiple processors.

20. vmstat

Vmstat is a tool that provides reporting virtual memory statistics. It covers the system’s memory, swap and processors utilization in real-time.

By default vmstat print as follows:

21. iostat

Iostat command is a command used to monitor the system’s input/output (I/O) device load by observing the time the devices are active in relation to their average transfer rates.

Tcpdump is a network troubleshooting command which is also known as a packet sniffer is used to capture and display packets from a network. To run tcpdump command you require root or user with sudo privileges.

When you use tcpdump without any option, it will analyze the traffic on all the interfaces.

Lsof command stands for ‘List Open Files’. It is extremely useful to find out the details about files that have been opened and their respective processes.

In order to see the list of files opened by some particular user, use -u option along with lsof commad.

Linux watch command can be used to repeat a command at regular intervals.

User Related Linux Commands

These commands are used to manage Linux users.

ID command is used in Linux to print real and effective User ID (UID) and Group ID (GID). An UID is a single identity for a user. While Group ID (GID) can consist of more than one UID.

Linux last command is used to check previously logged in user into your server.

Who command is a tool print information about users who are currently logged in. who command only see a real user who logged in. It won’t see a user that doing su command (switch user).

Groupadd command is used to create a group in Linux.

The following command will add a group ‘developers’ to the system.

In Linux and Unix-like operating systems, the most common command to create users is ‘useradd’ or ‘adduser’.

Let’s check how to create a new user ‘bill’ and set a password. The command adds an entry to the /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files.

Userdel is a low-level utility for removing users. Userdel command will look for the system account files such as ‘/etc/password’ and ‘/etc/group’ and then it will delete all entries related to the user name from there.

Linux usermod command is used to modify or change an existing user’s attributes. User’s attributes are home directory, shell, password expiration date, groups, UID etc.

Passwd is the command used in Linux (Redhat, Centos, Ubuntu, Debian, Fedora) and UNIX-like operating systems to change passwords.

File Related Linux Commands

These commands are used to handle files and directories.

A very simple yet powerful command used for listing files and directories. By default, ls command will list the content of the current directory.

Linux pwd is a command to print the name of the current working directory. When we are ‘lost’ into a deep directory, we can always reveal where we are.

Check the example below:

On Linux, we can use mkdir command to create a directory.

By default, running mkdir without any option, it will create a directory under the current directory.

36. cat

We use cat command mainly to view contents, concatenate and redirect the output of files. The easiest way to use cat is just type ‘cat file_name’.

The following examples of cat command will display Linux distribution name and which version is currently installed on the hardware.

When a file no longer needed, we may delete it to save storage space. On the Linux system, we can use rm command to do it.

Cp command is used in Linux to create copy of files and directories.

The following command will copy the file ‘myfile.txt’ from current directory to ‘/home/linoxide/office’.

When you want to move files from one place to another and you don’t want to duplicate it, we use mv command.

40.cd

Cd command is used to change the current working directory of a user in Linux and other Unix-like operating systems.

Symbolic link or soft link is a special type of file that contains a reference, which points to another file or directory. ln command is used to create symbolic links.

Ln command uses the following syntax:

Touch command is used in Linux to change for file access and modification time. We may use touch command in Linux to create an empty file.

More command is a command for displaying a long text file per page at a time.

The head command is used to print the first few lines of a text file. By default, the head command prints the first 10 lines of each file to standard output, which is the display screen.

45. tail

As you may know, cat command is used in displaying the entire content of a file via standard input. But in some cases, we have to print part of the file. By default, the tail command displays the last ten lines.

GPG is a tool used in linux for secure communication. It uses a combination of two keys (symmetric-key and public-key cryptography) for encrypting files.

Cksum command is used in Linux print CRC checksum and the number of bytes that are there in that file.

Читайте также:  Unetbootin windows 661 что это

Diff command is used to compare two files line by line from the terminal.

Sort command is used in Linux to sort the contents of a text file line by line.

Uniq is a command line tool used to report and filter duplicate lines from a file.

Linux wc command counts the number of bytes, characters, words, and lines in a file or in standard input.

Dir command is used to lists the content of a directory.

Linux tee command is used for chaining and redirection of tasks, you can redirect the output and/or errors to a file and it will not be shown on the terminal.

The tr (translate) command is used in Linux mainly for translating and deleting characters. It can be used to convert uppercase to lowercase, squeeze repeating characters and deleting characters.

Process Related Linux Commands

These commands are used to handle Linux processes

55. ps

Linux ps command is a built-in tool to capture current processes on the system. It will capture the system condition at a single time.

56. pmap

The Linux pmap command displays the memory usage map of a process or multiple processes. Pmap reports information about the address space or memory usage map of a process.

57. top

The top command in Linux displays the running processes on the system. It is used extensively for monitoring the load on a server.

58. kill

The kill command is used on Linux and other Unix-like operating systems to terminate processes without having to log out or reboot the computer.

59. killall

The killall command can be used to send a signal to a particular process by using its name. It means if you have five versions of the same program running, the killall command will kill all five.

60. pkill

Similar to killall command, pkill send signal to terminate a process with its name. From Centos 7, it encourages to use pkill.

61. fg and bg

We can run the jobs in background without any intervention from user. The processes that have been stopped by some interrupt signal can be continued in background with bg command. The fg command is like bg command except that instead of sending a command in the background, it runs them in foreground and occupies the current terminal and waits for process to exit.

File Permission Related Linux Commands

These commands are used to change permissions of the files

The file permissions are applied on three levels: the owner, group members and others. The chmod command is used to change these permissions.

For following example, 755 will set read+write+execute permission to the user, set read+execute permission to the group and set read+execute permission to others for the file ‘asciiquarium.tar.gz’.

The chown command in Linux is used to change the ownership and group ownership of files and directories.

In the following examples the owership of the file ‘/corpora/stopwords/danish.txt’ is change from ‘root:root’ to ‘raghu:altair’.

Chgrp command is used to changes the group ownership of a file unlike chown command that allows you to change the user and group ownership.

Network Related Linux Commands

These commands are used to view and edit network configurations related aspects of the system

The ifconfig command is used to display and set ip address to network interfaces in Linux. Now ifconfig is deprecated in favor of iproute2 (the ip command).

The linux ip command is similar to ifconfig, but more powerful and is intended to be a replacement for it. With ip you have the advantage of performing several network administration tasks with only one command.

The ethtool command is used to display/change Ethernet adapter settings. You can change network card speed, auto-negotiation, wake on LAN setting, duplex mode using this tool in Linux.

Both mii-tool and ethernet tool allows Linux system administrators to modify or change or check the network interface card.

69. ping and ping6

Ping and ping 6 commands are used to sends echo requests to the host to test ipv4 and ipv6 connection.

In Linux whois command is used to g ets information for a domain such as owner of the domain, the owner’s contact information, and nameservers.

Dig performs DNS lookups and displays the answers that are returned from the name server(s) that were queried.

Host command is a simple utility for performing DNS lookups in Linux. With this command, we can translate names into IP Addresses and vice versa.

Wget is the command line, noninteractive, free utility in Unix like Operating systems not excluding Microsoft Windows, for downloading files from the internet.

Netstat is a command-line tool that is used to view and monitor network statistics and configurations of a system. This tool is available on both Unix, Linux and Windows NT-based operating systems

Nslookup is a network administration tool for querying the Domain Name System to obtain domain name or IP address from DNS record.

Compression/Archive Related Commands

These commands are used to compress and decompress files.

Tar is a command line tool used to create and manipulate archive files in Linux and Unix systems. Tar command creates .tar archive file and then compress using gzip or bzip2.

Gzip command compresses files into a single file and suffix ‘.gz’.

Package Installation Related Commands

These commands are used to manage Linux packages.

78. rpm

RPM (Redhat Package Manager) is a command line package management utility used for installing, uninstalling, updating, querying and verifying software packages.

79. make

Make command is used to build executable programs and libraries from source code.

80. yum

Yum (Yellowdog Updater Modified) is a utility provided in RHEL based systems to install, remove and search packages. Yum installs the package dependencies automatically, for example, yum install httpd will install ‘apache’ server and it’s required dependencies automatically.

81. Apt

Apt is a command line tool for installing, updating, removing deb packages on Ubuntu, Debian, and related Linux distributions.

Search Related Linux Commands

These commands are used to search for files and patterns

The grep command allows searching for a text or string in a file or from output console of a command, for a term or pattern matching regular expressions.

The locate is a command line utility for searching files and directories in the entire filesystem.

The Linux find command is a very powerful command which is basically used for searching files under a directory structure using certain criteria.

SSH is used for logging into a remote machine and for executing commands on a remote machine. Whenever data is sent by a computer to the network, ssh will automatically encrypt.

Telnet is a command-line tool used to connect remote host manly to test application ports connectivity.

File Transfer Related Linux Commands

These commands are used to copy files from one system to another system
87. scp

SCP is a secure copy program to transfer files or directories between Linux hosts on the network. SCP uses ssh protocol to transfer the data.

Here, a local file ‘examples.txt’ is copied to a remote host.

$ scp examples.txt user@192.168.1.10:/home/user/account

Rsync synchronizes files and directories between local machines to the remote machine. It can recursively copy files and directory, copy symlinks, preserve (permissions, group, modification time and ownership) file identity.

Disk Use Related Linux Commands

These commands are used to view disk statistics.

The df command displays the disk space usage for the Linux filesystems. It displays the amount of total and free disk space available on the mounted file systems.

Fdisk command is used in Linux to view the existing partitions, create a new partition, delete or change existing partitions.

91. du

The du (disk usage) is the command used in Linux to check the directory size including its other contents as well as the size of individual files.

The command findmnt is used to list mounted filesystems in Linux. This command will look for a particular filesystem in /etc/fstab, /etc/mtab or /proc/self/mountinfo.

Mount is a command used in Linux to attached filesystems and drives and umount command is used to detach (unmount) any attached file systems or devices.

Conclusion

In this tutorial, we learned an overview of all most all Linux commands. For a final word, you can always rely on Linux man pages for more information. I hope this tutorial was helpful and please provide your suggestions in the below comment section.

Источник

Оцените статью