All linux command with example

Содержание
  1. 40 Basic Linux Commands used Frequently
  2. Linux Basic Commands
  3. 1) pwd command
  4. 2) cal command
  5. 3) echo command
  6. 4) date command
  7. 5) tty command
  8. 6) whoami command
  9. 7) id command
  10. 8) clear command
  11. Help command
  12. 9) help option
  13. 10) whatis command
  14. 11) Manual Pages
  15. 12) Info pages
  16. Linux Filesystem commands
  17. 13) Changing Directories Command
  18. 14) Listing File And Directories Command
  19. Creating files and directories
  20. 15) mkdir command
  21. 16) touch command
  22. Copy, move and remove commands
  23. 17) copy command
  24. 18) move command
  25. 19) To remove or Delete
  26. To remove files and directories
  27. Other file commands
  28. 20) file command
  29. 21) stat command
  30. 22) cat command
  31. 23) pagers
  32. 24) head command
  33. 25) tail command
  34. 26) wc command
  35. 27) grep command
  36. 28) ln command
  37. Text Editors
  38. 29) Pico & Nano
  39. 30) VI editor
  40. Useful commands
  41. 31) alias command
  42. 32) w command
  43. 33) last command
  44. 34) du command
  45. 35) df command
  46. 36) fdisk command
  47. 37) netstat command
  48. 38) history command
  49. 39) passwd command
  50. 40) Shutdown Command
  51. 35 Linux Basic Commands Every User Should Know
  52. Linux Basic Commands
  53. 1. pwd command
  54. 2. cd command
  55. 3. ls command
  56. 4. cat command
  57. 5. cp command
  58. 6. mv command
  59. 7. mkdir command
  60. 8. rmdir command
  61. 9. rm command
  62. 10. touch command
  63. 11. locate command
  64. 12. find command
  65. 13. grep command
  66. 14. sudo command
  67. 15. df command
  68. 16. du command
  69. 17. head command
  70. 18. tail command
  71. 19. diff command
  72. 20. tar command
  73. 21. chmod command
  74. 22. chown command
  75. 23. jobs command
  76. 24. kill command
  77. 25. ping command
  78. 26. wget command
  79. 27. uname command
  80. 28. top command
  81. 29. history command
  82. 30. man command
  83. 31. echo command
  84. 32. zip, unzip command
  85. 33. hostname command
  86. 34. useradd, userdel command
  87. Bonus Tips and Tricks
  88. To Sum Up

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.

Читайте также:  При загрузке windows потом нет сигнала

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.

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.

Читайте также:  Windows 10 размытый экран входа

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 .

Источник

35 Linux Basic Commands Every User Should Know

When hearing about Linux, most people think of a complicated operating system that is only used by programmers. But it’s not as scary as it seems.

Linux is an entire family of open-source Unix operating systems, that are based on the Linux Kernel. This includes all of the most popular Linux based systems like Ubuntu, Fedora, Mint, Debian, and others. More accurately, they’re called distributions or distros.

Since Linux was first released in 1991, it has continued to gain popularity due to its open-source nature. People can freely modify and redistribute it under their own name.

When operating a Linux OS, you need to use a shell — an interface that gives you access to the operating system’s services. Most Linux distributions use a graphic user interface (GUI) as their shell, mainly to provide ease of use for their users.

That being said, it’s recommended to use a command-line interface (CLI) because it’s more powerful and effective. Tasks that require a multi-step process through GUI can be done in a matter of seconds by typing commands into the CLI.

So if you’re considering using Linux, learning basic command lines will go a long way. In this article, you’ll learn 35 basic Linux commands that will undoubtedly help you navigate through Linux as a newbie.

Linux Basic Commands

Before we go on to the list of commands, you need to open the command line first. If you are still unsure about the command-line interface, check out this CLI tutorial.

Although the steps may differ depending on the distribution that you’re using, you can usually find the command line in the Utilities section.

Here is a list of basic Linux commands:

1. pwd command

Use the pwd command to find out the path of the current working directory (folder) you’re in. The command will return an absolute (full) path, which is basically a path of all the directories that starts with a forward slash (/). An example of an absolute path is /home/username.

2. cd command

To navigate through the Linux files and directories, use the cd command. It requires either the full path or the name of the directory, depending on the current working directory that you’re in.

Let’s say you’re in /home/username/Documents and you want to go to Photos, a subdirectory of Documents. To do so, simply type the following command: cd Photos.

Another scenario is if you want to switch to a completely new directory, for example,/home/username/Movies. In this case, you have to type cd followed by the directory’s absolute path: cd /home/username/Movies.

There are some shortcuts to help you navigate quickly:

  • cd .. (with two dots) to move one directory up
  • cd to go straight to the home folder
  • cd- (with a hyphen) to move to your previous directory

On a side note, Linux’s shell is case sensitive. So, you have to type the name’s directory exactly as it is.

3. ls command

The ls command is used to view the contents of a directory. By default, this command will display the contents of your current working directory.

If you want to see the content of other directories, type ls and then the directory’s path. For example, enter ls /home/username/Documents to view the content of Documents.

There are variations you can use with the ls command:

  • ls -R will list all the files in the sub-directories as well
  • ls -a will show the hidden files
  • ls -al will list the files and directories with detailed information like the permissions, size, owner, etc.

4. cat command

cat (short for concatenate) is one of the most frequently used commands in Linux. It is used to list the contents of a file on the standard output (sdout). To run this command, type cat followed by the file’s name and its extension. For instance: cat file.txt.

Here are other ways to use the cat command:

  • cat > filename creates a new file
  • cat filename1 filename2>filename3 joins two files (1 and 2) and stores the output of them in a new file (3)
  • to convert a file to upper or lower case use, cat filename | tr a-z A-Z >output.txt

5. cp command

Use the cp command to copy files from the current directory to a different directory. For instance, the command cp scenery.jpg /home/username/Pictures would create a copy of scenery.jpg (from your current directory) into the Pictures directory.

6. mv command

The primary use of the mv command is to move files, although it can also be used to rename files.

The arguments in mv are similar to the cp command. You need to type mv, the file’s name, and the destination’s directory. For example: mv file.txt /home/username/Documents.

To rename files, the Linux command is mv oldname.ext newname.ext

7. mkdir command

Use mkdir command to make a new directory — if you type mkdir Music it will create a directory called Music.

There are extra mkdir commands as well:

  • To generate a new directory inside another directory, use this Linux basic command mkdir Music/Newfile
  • use the p (parents) option to create a directory in between two existing directories. For example, mkdir -p Music/2020/Newfile will create the new “2020” file.

8. rmdir command

If you need to delete a directory, use the rmdir command. However, rmdir only allows you to delete empty directories.

9. rm command

The rm command is used to delete directories and the contents within them. If you only want to delete the directory — as an alternative to rmdir — use rm -r.

Note: Be very careful with this command and double-check which directory you are in. This will delete everything and there is no undo.

10. touch command

The touch command allows you to create a blank new file through the Linux command line. As an example, enter touch /home/username/Documents/Web.html to create an HTML file entitled Web under the Documents directory.

11. locate command

You can use this command to locate a file, just like the search command in Windows. What’s more, using the -i argument along with this command will make it case-insensitive, so you can search for a file even if you don’t remember its exact name.

To search for a file that contains two or more words, use an asterisk (*). For example, locate -i school*note command will search for any file that contains the word “school” and “note”, whether it is uppercase or lowercase.

12. find command

Similar to the locate command, using find also searches for files and directories. The difference is, you use the find command to locate files within a given directory.

As an example, find /home/ -name notes.txt command will search for a file called notes.txt within the home directory and its subdirectories.

Other variations when using the find are:

  • To find files in the current directory use, find . -name notes.txt
  • To look for directories use, / -type d -name notes. txt

13. grep command

Another basic Linux command that is undoubtedly helpful for everyday use is grep. It lets you search through all the text in a given file.

To illustrate, grep blue notepad.txt will search for the word blue in the notepad file. Lines that contain the searched word will be displayed fully.

14. sudo command

Short for “SuperUser Do”, this command enables you to perform tasks that require administrative or root permissions. However, it is not advisable to use this command for daily use because it might be easy for an error to occur if you did something wrong.

Читайте также:  Поменять пользователя файла linux

15. df command

Use df command to get a report on the system’s disk space usage, shown in percentage and KBs. If you want to see the report in megabytes, type df -m.

16. du command

If you want to check how much space a file or a directory takes, the du (Disk Usage) command is the answer. However, the disk usage summary will show disk block numbers instead of the usual size format. If you want to see it in bytes, kilobytes, and megabytes, add the -h argument to the command line.

17. head command

The head command is used to view the first lines of any text file. By default, it will show the first ten lines, but you can change this number to your liking. For example, if you only want to show the first five lines, type head -n 5 filename.ext.

18. tail command

This one has a similar function to the head command, but instead of showing the first lines, the tail command will display the last ten lines of a text file. For example, tail -n filename.ext.

19. diff command

Short for difference, the diff command compares the contents of two files line by line. After analyzing the files, it will output the lines that do not match. Programmers often use this command when they need to make program alterations instead of rewriting the entire source code.

The simplest form of this command is diff file1.ext file2.ext

20. tar command

The tar command is the most used command to archive multiple files into a tarball — a common Linux file format that is similar to zip format, with compression being optional.

This command is quite complex with a long list of functions such as adding new files into an existing archive, listing the content of an archive, extracting the content from an archive, and many more. Check out some practical examples to know more about other functions.

21. chmod command

chmod is another Linux command, used to change the read, write, and execute permissions of files and directories. As this command is rather complicated, you can read the full tutorial in order to execute it properly.

22. chown command

In Linux, all files are owned by a specific user. The chown command enables you to change or transfer the ownership of a file to the specified username. For instance, chown linuxuser2 file.ext will make linuxuser2 as the owner of the file.ext.

23. jobs command

jobs command will display all current jobs along with their statuses. A job is basically a process that is started by the shell.

24. kill command

If you have an unresponsive program, you can terminate it manually by using the kill command. It will send a certain signal to the misbehaving app and instructs the app to terminate itself.

There is a total of sixty-four signals that you can use, but people usually only use two signals:

  • SIGTERM (15) — requests a program to stop running and gives it some time to save all of its progress. If you don’t specify the signal when entering the kill command, this signal will be used.
  • SIGKILL (9) — forces programs to stop immediately. Unsaved progress will be lost.

Besides knowing the signals, you also need to know the process identification number (PID) of the program you want to kill. If you don’t know the PID, simply run the command ps ux.

After knowing what signal you want to use and the PID of the program, enter the following syntax:

kill [signal option] PID.

25. ping command

Use the ping command to check your connectivity status to a server. For example, by simply entering ping google.com, the command will check whether you’re able to connect to Google and also measure the response time.

26. wget command

The Linux command line is super useful — you can even download files from the internet with the help of the wget command. To do so, simply type wget followed by the download link.

27. uname command

The uname command, short for Unix Name, will print detailed information about your Linux system like the machine name, operating system, kernel, and so on.

28. top command

As a terminal equivalent to Task Manager in Windows, the top command will display a list of running processes and how much CPU each process uses. It’s very useful to monitor system resource usage, especially knowing which process needs to be terminated because it consumes too many resources.

29. history command

When you’ve been using Linux for a certain period of time, you’ll quickly notice that you can run hundreds of commands every day. As such, running history command is particularly useful if you want to review the commands you’ve entered before.

30. man command

Confused about the function of certain Linux commands? Don’t worry, you can easily learn how to use them right from Linux’s shell by using the man command. For instance, entering man tail will show the manual instruction of the tail command.

31. echo command

This command is used to move some data into a file. For example, if you want to add the text, “Hello, my name is John” into a file called name.txt, you would type echo Hello, my name is John >> name.txt

32. zip, unzip command

Use the zip command to compress your files into a zip archive, and use the unzip command to extract the zipped files from a zip archive.

33. hostname command

If you want to know the name of your host/network simply type hostname. Adding a -i to the end will display the IP address of your network.

34. useradd, userdel command

Since Linux is a multi-user system, this means more than one person can interact with the same system at the same time. useradd is used to create a new user, while passwd is adding a password to that user’s account. To add a new person named John type, useradd John and then to add his password type, passwd 123456789.

To remove a user is very similar to adding a new user. To delete the users account type, userdel UserName

Bonus Tips and Tricks

Use the clear command to clean out the terminal if it is getting cluttered with too many past commands.

Try the TAB button to autofill what you are typing. For example, if you need to type Documents, begin to type a command (let’s go with cd Docu, then hit the TAB key) and the terminal will fill in the rest, showing you cd Documents.

Ctrl+C and Ctrl+Z are used to stop any command that is currently working. Ctrl+C will stop and terminate the command, while Ctrl+Z will simply pause the command.

If you accidental freeze your terminal by using Ctrl+S, simply undo this with the unfreeze Ctrl+Q.

Ctrl+A moves you to the beginning of the line while Ctrl+E moves you to the end.

You can run multiple commands in one single command by using the “;” to separate them. For example Command1; Command2; Command3. Or use && if you only want the next command to run when the first one is successful.

To Sum Up

Basic Linux commands help users execute tasks easily and effectively. It might take a while to remember some of the basic commands, but nothing is impossible with lots of practice.

In the end, knowing and mastering these basic Linux commands will be undoubtedly beneficial for you. Good luck!

Artūras is an experienced technical content writer. Bringing in a lot of knowledge about WordPress and web hosting to the team, he strives to write pristine content about any IT related subject. He also loves dogs.

Источник

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