- 10 basic Linux commands you need to know
- ls lists directory contents
- More Linux resources
- 40 Basic Linux Commands used Frequently
- Linux Basic Commands
- 1) pwd command
- 2) cal command
- 3) echo command
- 4) date command
- 5) tty command
- 6) whoami command
- 7) id command
- 8) clear command
- Help command
- 9) help option
- 10) whatis command
- 11) Manual Pages
- 12) Info pages
- Linux Filesystem commands
- 13) Changing Directories Command
- 14) Listing File And Directories Command
- Creating files and directories
- 15) mkdir command
- 16) touch command
- Copy, move and remove commands
- 17) copy command
- 18) move command
- 19) To remove or Delete
- To remove files and directories
- Other file commands
- 20) file command
- 21) stat command
- 22) cat command
- 23) pagers
- 24) head command
- 25) tail command
- 26) wc command
- 27) grep command
- 28) ln command
- Text Editors
- 29) Pico & Nano
- 30) VI editor
- Useful commands
- 31) alias command
- 32) w command
- 33) last command
- 34) du command
- 35) df command
- 36) fdisk command
- 37) netstat command
- 38) history command
- 39) passwd command
- 40) Shutdown Command
10 basic Linux commands you need to know
Whether you’re a Windows system administrator looking to expand your skills into Linux, a fresh convert to Linux, or someone who’s looking to find a job in IT, this introduction to some common Linux commands is for you. These 10 commands are the ones that Linux system administrators use frequently—in fact, they use at least eight of them on a daily basis. And no matter how long you use Linux, you’ll always use these commands. Some of you MS-DOS users will recognize a few of these and, not surprisingly, they have the same function in both operating systems.
ls lists directory contents
More Linux resources
The list ( ls ) command is equivalent to the DOS DIR command, in that it lists files and directories. If you simply type ls at a prompt ( $ ), you’ll see all non-hidden files in your current directory, which is your home directory when you first log into a Linux system. The ls command won’t show you much in your home directory on a new system, so let’s explore a directory that contains a lot of files and directories: /etc . The /etc (et-see) directory is where a Linux system’s configuration files live.
A large number of files (over 200) appear on your screen. You’ve successfully listed the contents of the /etc directory, but you can actually list files in several different ways. Above, I mentioned non-hidden files. In your home directory, where you are now, you probably have hidden files. Hidden files in Linux begin with a period ( . ). For example, you likely have a .bash_profile file there. To see it, use the following ls command.
You now see several files beginning with a period. The -a switch—or option, as it’s called—shows you all files, even hidden ones.
man displays manual pages
Linux has an extensive set of online documentation for your reference. They’re referred to as manual pages, as in read the manual. The abbreviated command for referencing this documentation is, man and a screen-full of information appears before you.
It’s easy to navigate man pages. Use the Enter key to advance one line at a time, the ‘ b ‘ key to go back, the Space bar to advance a full-screen page, and the ‘ q ‘ key to exit the man page. As an example, look at the man page for the ls command.
cat concatenates files
The cat command is important as a basic command because it serves two very important functions: concatenating (merging) files (as the name suggests) and printing the contents of a file to the screen. Printing the contents of files is by far the more frequent use of this command. If you want to see a file’s contents, use the following format:
For example, you might type the following to display the contents of the system’s passwd file on the screen:
To use cat for its file concatenation powers, the general form of the command is:
$ cat file1 file2 > file1file2
For example, to redirect the contents of grocerylist.txt and todo_list.txt into the Saturday.txt file:
$ cat grocerylist.txt todo_list.txt > Saturday.txt
You can concatenate as many files as you want into a single file using cat .
touch changes file timestamps
The touch command is another one that serves a dual purpose. Its designated purpose is to update the timestamps on files. If you list the contents of a directory in long format with:
The command’s output displays the permissions, ownership, size, created or last accessed date/time, and the filename:
-rw-rw-r—. 1 khess khess 175 Jul 23 19:19 all.txt
-rw-rw-r—. 1 khess khess 61 Jul 23 19:17 new.txt
-rw-rw-r—. 1 khess khess 114 Jul 23 19:09 students.txt
Use touch to update the last accessed timestamp:
-rw-rw-r—. 1 khess khess 175 Jul 23 19:19 all.txt
-rw-rw-r—. 1 khess khess 61 Jul 26 11:28 new.txt
-rw-rw-r—. 1 khess khess 114 Jul 23 19:09 students.txt
Using touch to update last accessed time is actually an infrequent use of this command. The common use for touch is to create an empty file as a placeholder. Some programs require that a file exists to operate correctly, and this is one method of kickstarting such a process. Otherwise, this use offers a quick way to create a file without opening a text editor and then saving an empty file:
-rw-rw-r—. 1 khess khess 175 Jul 23 19:19 all.txt
-rw-rw-r—. 1 khess khess 61 Jul 26 11:28 new.txt
-rw-rw-r—. 1 khess khess 114 Jul 23 19:09 students.txt
-rw-rw-r—. 1 khess khess 0 Jul 26 11:53 today.txt
You have created a new empty file, today.txt .
pwd prints the working directory
The pwd command is your Linux system’s compass, in that it tells you where you are. It has no other function than supplying that bit of information to you. Try the following, and you will see that you’re in your home directory, which is shown in the format /home/ :
$ pwd
/home/khess
If you get lost, or just wonder where you are in the filesystem, this is the command that will tell you. Linux users use it frequently before changing or removing files to be sure of their current location.
The pwd command always displays the full path to your location, even if you’re multiple directories deep from the root ( / ) directory, which is why I see /home/khess rather than khess or /khess .
cd changes directory
Very closely related to the pwd command is the cd command. Changing directories is a frequent activity on a Linux system. As stated before, when you first log in, you’re placed into your home directory. Every user on a Linux system has a home directory. Regular user accounts have personal directories under the /home directory. Your home directory is under /home/ . To view all user’s home directories, cd to the /home directory.
What you see here depends on your system. If you are the only user on a personal system, you will only see your home directory. Production systems might have hundreds of user accounts on them. The quick way to return to your home directory, no matter where you are on the system, is to type cd with no arguments or directory paths:
So, if you ever get «lost» on the system and need to reset your bearings, type cd and you’ll be placed safely into your home directory. You can cd to almost any directory on the system by supplying its full path after the cd command:
To change directory to the one above your current directory, use the double period (dot) argument:
Now you are in the /usr directory. Remember that you can «prove» your location to yourself by issuing the pwd command:
There are times when you don’t need to cd to a particular directory. You can read a file from your current location if you supply the full path to the file you’re interested in viewing. For example, you don’t need to cd to the /usr/bin directory to issue the pwd command. You issue it from your current location because it is in your path.
The path is a more advanced topic for another article, but just be aware that you don’t need to cd to do everything. The time to cd is when you will be working in a specific directory for some reason. Otherwise, you can do what you need to do from your home directory. You’ll find out why changing directories can be a bad thing in the next section.
rm removes files and directories
The rm command removes (deletes) files and directories. One of the quirks of Linux that you’ll find different from DOS/Windows is that it isn’t chatty, which means that when you remove a file or directory, you won’t (by default) receive a message such as, «Are you sure?» It just isn’t the Linux way. There is a recommended workaround for that behavior that I’ll show you later in this section.
For now, let’s remove the today.txt file that you created earlier with the touch command:
Did you notice that you didn’t receive any questions or prompts? Linux assumes you know what you want to do before you hit the Enter key. That’s a little disconcerting, isn’t it? Ask Linux system administrators if any files have ever gone missing during one of their sessions. I’ll put money on an affirmative response and I’m not a gambler. You can work around this non-interactive behavior of certain commands by placing a -i switch (option) after the command. Try the following example:
$ rm -i newfile.txt
rm: remove regular empty file ‘newfile.txt’?
The -i makes rm interactive. Answer with a y and the file goes away. Answer with an n and you keep the file. To be safe, you can always use the -i switch with rm . There’s no harm or shame in it and you’ll be glad you did at some point in the future.
cp copies files and directories
Copying files and directories is a very common task for Linux system administrators. There’s no great secret to its usage and you simply issue the copy ( cp ) command, the file or directory source, and the destination. To copy a file, file.txt , to the /opt/files directory, use:
$ cp file.txt /opt/files
To copy an entire directory and all its contents, including subdirectories, use the -R (Recurse) option. Copy the data directory in your home directory to /opt/files . You can use either the -r or -R to recurse copy files:
$ cp -R data /opt/files
The cp command is rare in that both the upper- and the lowercase options for an action are the same. Of course, you can use wildcards when copying files to filter them with patterns:
$ cp *.txt /opt/files
mkdir makes directories
If you’re an organized person, you’ll want to create directories to satisfy your need to properly arrange your files and data into separate compartments (directories). It’s easy to create directories. Issue the mkdir command followed by the directory name you wish to create:
If you’re even more organized and you’ve done some planning, you can create a whole hierarchy of directories with one command. You want to create a data directory that includes subdirectories for documents, forms, tests, and outgoing. Why issue multiple commands when you can do it all at once:
$ mkdir -p data/documents/forms/tests/outgoing
The -p option tells the system that you are creating a parent directory and subdirectories. Check your work using the ls command. You can also create multiple directories at the same level all at once.
$ mkdir docs spreadsheets email old
Use the ls command to be sure the mkdir command did what you wanted it to do.
ps lists the current running processes
The last of the 10 basic Linux commands you need to know is ps . This command shows you currently running processes. If you issue the ps command, you will only see your own processes:
$ ps
PID TTY TIME CMD
7505 pts/0 00:00:00 bash
18119 pts/0 00:00:00 ps
If you’re not running anything, then this output is not very interesting. It’s far more interesting to see what’s going on system-wide. You can do this by adding some options to ps . The most valuable options are -e and -f , for every (all) and full format, respectively. To get the most information from the ps command, combine the two options into the following command. I’ve included the first few lines from the output of ps -ef from my system for you:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jul23 ? 00:00:25 /usr/lib/systemd/systemd —system —deserialize 20
root 2 0 0 Jul23 ? 00:00:00 [kthreadd]
root 3 2 0 Jul23 ? 00:00:00 [rcu_gp]
root 4 2 0 Jul23 ? 00:00:00 [rcu_par_gp]
root 6 2 0 Jul23 ? 00:00:00 [kworker/0:0H-kblockd]
root 8 2 0 Jul23 ? 00:00:00 [mm_percpu_wq]
The fields are simple to understand and useful when troubleshooting performance problems:
Field | Description |
---|---|
C | CPU Usage. |
CMD | The command or process name with path. |
PID | Process ID. |
PPID | Parent Process ID: The parent process is the one that spawned the process. |
STIME | Start Time for the process. |
TIME | CPU Time for the process. |
TTY | The user terminal that spawned the process. System process will show a ?. |
UID | User ID of the process owner. |
There are other options you can use with the ps command, and it seems everyone has a preference, but the two most popular are: ps -ef and ps aux . They both provide you with a lot of process information.
There you have the 10 basic Linux commands you need to know. There isn’t one command that’s more important than any other. They’re all important and they’re all useful. I chose these because they are the 10 commands that everyone regularly uses whether you’re a fresh newbie or an old salty system administrator from the dark days before Linux.
If you’re interested in how DOS and Linux commands compare, check out this: Comparison of Common DOS and Linux Commands.
[Want to try out Red Hat Enterprise Linux? Download it now for free.]
Источник
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.
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 .
Источник