40 Essential Linux Commands You Should Know

Last Updated: September 30, 2016

In the context of operating systems in general, and Linux in particular, the term “command” means either a command line application or functionality built into the user shell. However, to the end user, this distinction is of little consequence. Both are used in the same way. You input words into your terminal emulator, and it outputs the results.

The goal of this article is to list some commands every Linux user should know, or at least know of, in the case of those with a phobia of text-based interfaces. It doesn’t mean to list every useful command, it isn’t a list of the lesser known utilities, and it isn’t a manual. It aims for coverage of the most useful application in day to day life.

As such, it’s divided into several categories, corresponding to particular tasks. It presumes no particular distribution, and while not all the programs described will be installed by default in every distribution, most of them will be present, and the others can be found in the repositories.

Linux Commands For File System Management

1. ls

By default, list the contents of the current directory. If you provide it a path, it will list the contents of that. Useful options to know are -l and -a, a long list format with more information and show hidden (dot) files, respectively.

2. cat

If given a single file, prints its contents to the standard output. If you give it more than one file, it will concatenate them, and you can then redirect the output into a new file. Potentially useful is the -n option, which numbers the lines.

3. cd

Allows you to go from current directory to specified directory. Calling it without arguments returns you to your home directory. Calling it with two dots (cd ..) returns you to a directory “above” the current one, while calling it with a dash (cd -) returns you to the previous directory, regardless of where it’s located relative to the current one.

4. pwd

Prints your current directory. Useful if your prompt doesn’t contain this information, and especially useful in BASH programming for obtaining a reference to the directory in which you’re executing the code.

5. mkdir

Create new directories. The most handy switch is -p, which creates the entire specified structure if it doesn’t exist already.

6. file

Tells you the type of a file. Since files in Linux aren’t under obligation to have extensions for the system to work (not that having extensions always helps), sometimes it’s hard for the user to know what type of file something is, and this little utility solves that problem.

7. cp

Copies files and directories. Since it doesn’t copy directories recursively by default, remember to use -r or -a. The latter preserves mode, ownership and time stamp info in addition to recursively copying.

8. mv

Moves or renames files and directories. Essentially, moving and renaming is one operation – renaming is just “moving” a single file to the same place under a different name.

9. rm

Delete files and directories. Certainly a very useful command to know, as you cannot remove clutter without it. However, beware when using it. Although nowadays you’d really have to work on it to cause some damage to the system, you can still damage yourself – rm doesn’t remove files to some imaginary wastebasket from which you can fish them out later when you realize you’ve made a horrible mistake, and “rm ate my homework” isn’t going to convince anyone. Deleting directories requires recursive operation, so once again we have the -r switch.

10. ln

Creates hard or symbolic links between files. Symbolic or soft links are sort of like Windows shortcuts, they provide a convenient way of accessing a particular file, though the analogy doesn’t quite hold – symlinks can point to anything, but do not feature any metadata. You aren’t very likely to ever use hard links, but knowing they’re aliases to files – as opposed to symlinks, which are aliases to file names – can’t hurt.

11. chmod

Change user permissions. This refers to viewing, writing and executing files. A normal user may change permissions for files he owns.

12. chown

Change file ownership. Only the root user may change the owner of a file. To recursively change the owner for all the files in a directory, use it with -R.

13. find

Search the filesystem for files or directories. Find is a very versatile and powerful command, not only because of its searching capabilities, but also because it allows you to execute arbitrary commands on matching (or non-matching, even) files.

14. locate

Unlike find, locate searches the updatedb database for file name patterns. This database contains a snapshot of the filesystem. This makes locate very fast, but also unreliable – it cannot tell whether anything changed since the last snapshot.

15. du

Show file or directory size. Among the more useful options are -h, which converts the reported sizes into a more human-friendly format, -s which gives only a summary instead of the whole listing, and -d which controls the depth of directory recursion.

16. df

Show disk usage. The default output is good enough – it lists every filesystem, reports its size and the amount of used and available space – but you might want to tack on -h, which once again provides a more human-friendly report.

17. dd

Convert and copy a file, according to its manpage. Not exactly the clearest or most useful description around, and yet, that’s all dd does. You give it a source and a destination, and optionally some other commands, and it copies from one to the other. Its power comes from the flexibility – you can tell it the exact block size, it can copy around corrupted data, and it isn’t picky about devices – if you want to overwrite your hard drive with zeroes straight from /dev/zero, you’re welcome to do it. It’s also commonly used for creating live USB sticks from hybrid ISO images.

18. mount / umount

This pair takes care of mounting and unmounting filesystems. This can range from USB sticks to ISO images. Usually only root has mounting privileges.

Linux Commands For Text Processing

19. more / less

These two similar utilities allow you to view text chunked into screenfuls. Imagine a very long output from some command. Perhaps you called cat on a file and your terminal emulator took a few seconds to scroll all the text. Well, if you pipe it into one of these, you can now scroll it at your leisure. Less is newer and offers more options, so there is no reason to use more.

20. head / tail

Another pair, but here both halves have their uses. Head outputs a number of the first (“head”) lines of a file, while tail outputs a number of last (“tail”) lines of a file. The default number is ten, but this can be controlled via the -n option. Another useful switch is -f, which is short for “follow”, which continually outputs any appended lines – so, for instance, if you wanted to monitor a log file instead of constantly opening and closing it, you could use “tail -f /path/to/logfile”.

21. grep

Grep, like all good Unix tools, does one thing, but does it well. It searches text for patterns. By default it looks at standard input, but you can specify files to be searched. A pattern can be a normal string or a regular expression. It can print out matching or non-matching lines, and their context. Every time you run a command which spews a lot of information you don’t need, pipe it into grep and let it do its magic.

22. sort

Sorts lines of text by various criteria. Among the more useful, there’s -n, which sorts by the numeric value of a string, and -r, which reverses the output. An example of where this might come in handy is sorting du output – for example, if you wanted to see the files sorted in descending order according to size, you’d combine the two options.

23. wc

The command line word counting utility. And line counting. And byte counting. And character counting.

24. diff

Shows the difference between two files via line by line comparison. It only shows altered lines, abbreviating changed as c, deleted as d and added as a.

Linux Commands For Process Management

25. kill / xkill / pkill / killall

All of these serve to “kill” a process, ie terminate it. The difference is what they accept as input. Kill wants the process ID, xkill allows you to click a window to close it, while killall and pkill accept the name of a process, but have somewhat different options and subtly different behavior. Note these do not belong to the same package, and xkill especially is not likely to be installed by default. We advise you to rectify that for your own convenience.

26. ps / pgrep

As mentioned, kill needs the process ID. One way to obtain this is by using ps, which prints information about the currently active processes. The default output is not hugely useful, so stick an -e there to see information about every process on the system. This is only a snapshot, it will not update, see top for that. The pgrep command works in the following manner: you give it a process name, it gives you the process ID. Partial matches count, so be careful.

27. top / htop

These two are similar, both display processes, and can be thought of as console system monitors. We recommend you install htop the first chance you get if your distribution doesn’t ship it by default, as it’s a much improved version of top. For starters, it’s not merely a viewer – it allows you to control processes via its user-friendly console GUI interface.

28. time

Time a process. Think of it as a stopwatch for program execution. Useful if you’re curious how much slower is your homework implementation of a sorting algorithm compared to the built-in one. Contrary to what you might expect based on the name, it doesn’t tell you the time. See date for that.

Linux Commands For BASH and User Environment

29. su / sudo

Su and sudo are two ways of accomplishing the same thing – running a command as another user. Depending on what your distribution is, you’ve likely seen only one or the other, but both are serviceable. The difference is that su switches you to a different user, while sudo only runs the command with another user’s privileges.

30. date

Unlike time, date does exactly what you’d expect it – it prints out the date (and time) to the standard output. The output itself can be formatted to your specification, and it takes everything from the usual stuff like year, month, day,
12 or 24 hour format to nanoseconds and the ISO week number. For example, date +”%j %V” would give you the day of the year followed by the ISO week number.

31. alias

This commands creates or changes aliases to other commands. What this means is, you can give names to new commands (or groupings of commands) or “rename” existing ones. It’s very handy for abbreviating long strings of commands you find yourself using often, or giving more memorable names to things you don’t use that often and have problems memorizing.

32. uname

Outputs some basic system information. By itself, it won’t give you anything very useful (“Linux”), but call it with -a, and it will provide kernel information, as well as tell you the hostname and processor architecture.

33. uptime

Tells you how long has the system been running. Not exactly essential information, but good for bragging rights and the occasional compute-things-relative-to-how-long-I’ve-been-at-the-computer situation.

34. sleep

You might be wondering why or how would this ever be useful, but even outside BASH scripts, it has its uses: for example, if you’d like to shutdown the computer after a certain period of time, or even as a makeshift alarm.

Linux Commands For User Management

35. useradd, userdel, usermod

These commands allow you do add, delete and modify user accounts. It’s not very likely you’ll be using these often, especially if you’re the sole user of your system, and even if not, you might opt for doing this via a GUI, but it’s good to know what they do and that they’re there in case you suddenly need them.

36. passwd

This command enables you to change your user account password. As root, you can reset normal user passwords, though you cannot view them. It’s a good security practice to change your password every so often.

Linux Commands Fro Help / Documentation

37. man / whatis

The man command brings up the manual for a particular command. Most command line applications come with a man page. Whatis provides a one line summary lifted from the relevant sections of the manual. What are sections of the manual? See for yourself with “man man”.

38. whereis

Tells you where an executable binary files lives, provided it’s in your path. It can also find its manual page and source code, provided they are present.

Linux Commands For Network

39. ip

If the list of network related commands seems awfully short, you’re probably not acquainted with ip. In short, the net-utils package which contains ipconfig, netstat and others has been deprecated in favor of the iproute2 package. It provides the ip command, which replaces ipconfig, netstat, route, etc. You could view it as a Swiss Army knife of networking, or a unwanted mess, but either way, it’s the future.

40. ping

Pings are ICMP ECHO_REQUEST datagrams, but that’s not important. The important thing is that the ping utility is a useful diagnostic tool. It allows you to quickly test if you’re connected to your router or the Internet, and gives some indication of the quality of that connection.

SEE ALSO: 10 Best Linux Desktop Environments

SHARE


Ivana Isadora is a freelance writer and translator working in English, Croatian and Swedish. She's a Linux user & KDE fan interested in startups, productivity and personal branding. She loves discovering new apps and presenting them to the world.

LEAVE A REPLY