Top 10 Basic Linux Commands
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.
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.
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 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.
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.
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.
The touch command is another one that serves a dual purpose. Its designated purpose is to update the timestamps on files.
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:
You have created a new empty file, today.txt.
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/:
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.
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:
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.
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 filename
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:
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.
The fields are simple to understand and useful when troubleshooting performance problems:
|CMD||The command or process name with path.|
|PPID||Parent Process ID: Parent process spawns a 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 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.
Top 10 Basic Linux Commands - Network Nuts
These top 10 basic linux commands will help you become a better system administrator. Free Linux knowledge with the Network Nuts blog. Read now!
Course Provider: Organization