Top 10 most useful bash commands
21/04/2009When I first started using Linux a few years ago, I could not for the life of me understand why anyone would want to use the command line for anything as long as there was a graphical alternative. After all, I belong to the group of people who grew up with graphical interfaces. Sure I’ve enjoyed more than my share of DOS games and text-based MUDs, but that command line business was very scary to me up until just a few years ago.
Then I started using Linux. In the beginning I always used graphical applications for everything from managing files and directories to setting file permissions, but somehow, slowly but surely I started noticing that some things could be done so much easier and faster if done in the command line, compared to the graphical alternative - and then I still didn’t know about scripting! Once I figured out that you could automate tasks by writing bash scripts, I was in love.
Throughout this article I will list and explain what I think are the ten most useful bash commands. Whether you’re a bash guru or a command-line newbie, keep reading to see if there are any commands that could be useful to you.
Before we start
You should probably know that I’m not listing these in any particular order. All of these are potentially useful commands, and whether number 1 is m0re or less useful is up to you to decide.
1. sed
A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is seds ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
As you can see above, sed is used to alter text from some kind of input stream. So let’s say that you want to print every line in a script, but you don’t want the comments to be included. Sed could do that for you.
sed -n -e '/^#/p' /path/to/my/file | more
Sed could also be configured to edit files, delete lines, or do pretty much anything you want it to. This could be hugely helpful when scripting or batch editing files. For more information on sed and how to properly use it, go here.
2. split
split - split a file into pieces
Split does exactly what you’d think it does. It splits larger files into several smaller ones. However, joining them back together is a little tricky, since there’s no simple command to join them together. However, I found this script on some blog. Using this, you could easily put them back together again:
#!/bin/bash
prefix=$1
outfile=$2
files="ls -m $prefix*|tr -d ,"
cat "echo $files" >> $outfile
file $outfile
I can’t use the syntax highlighter for this, for some reason, as it replaces my > with the ascii equivalent.
Just invoke it with this command where $1 is the prefix you entered when splitting the files, and $2 is the name of the file that is put together.
./script-name.sh $1 $2
3. man
man is the systems manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections, following a pre-defined order and to show only the first page found, even if page exists in several sections.
Most of you already know that man is an interface to the online manuals for bash commands. What I really wanted to show you was that you don’t actually have to read the manuals in the terminal. You can read them in your favorite text editor as well, which increases the readability tenfold (at least I think so). Just run this command:
man bash_command > filename
I can’t use the syntax highlighter for this, for some reason, as it replaces my > with the ascii equivalent.
With that you save the output of the man command to a textfile. If you want to append the output to a file that already exists, use >> instead of >.
4. touch
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty.
A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.
Touch is great for scripting. Let’s say you want to create an empty file (perhaps for saving settings, or something) if one doesn’t exist. You could write an if statement that checks whether the file exists, and creates one if it doesn’t. But touch saves you that trouble, plus, it looks a whole lot neater than using a conditional statement for something so small.
5. mv
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Many of you probably already know that you can use mv to move files around. What some of you might not know is that it’s also used to rename files. All you have to do is enter a different filename for the destination than the source, and the file will change name.
6. ln
ln - make links between files
Just like it says, ln is used to create links between files. This could be useful in many ways, but one in particular comes to mind. If you’re running a server and want to share some files that you yourself are using and want easy access to as well. Instead of having to put those files in the public directory, you can just make a link to them and put that link in the public directory. Easy as pie.
7. xkill & killall
Xkill is a utility for forcing the X server to close connections to clients. This program is very dangerous, but is useful for aborting programs that have displayed undesired windows on a user’s screen. If no resource identifier is given with -id, xkill will display a special cursor as a prompt for the user to select a window to be killed. If a pointer button is pressed over a non-root window, the server will close its connection to the client that created the window.
killall - kill processes by name
xkill and killall are both great tools to kill unwanted programs and processes. If you just run xkill in your terminal your mouse pointer will change to an X icon. Once you click on a window, that program will be killed (it’s not quite as satisfying as it sounds, sadly). killall works in a similar way, but instead of clicking on a window you’ll have to enter the process name or id to have it killed.
8. grep
grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
Everybody loves grep, and I can certainly see why. grep is used to search through files or input streams for lines matching an entered pattern. For example, if you’re throwing an enormous Linux install party (being that it’s such an exclusive event, you have to be on the guestlist to be allowed in), and you need to see if someone is on the guestlist, all you need to do is throw it to grep and ask it to find all lines with a certain name in it. For example:
grep -i -F "Joe Schmoe" guestlist.txt
Of course you can also use regular expressions for more advanced searching.
9. chown
chown changes the user and/or group ownership of each given file. If only an owner (a user name or numeric user ID) is given, that user is made the owner of each given file, and the files’ group is not changed. If the owner is followed by a colon and a group name (or numeric group ID), with no spaces between them, the group ownership of the files is changed as well. If a colon but no group name follows the user name, that user is made the owner of the files and the group of the files is changed to that user’s login group. If the colon and group are given, but the owner is omitted, only the group of the files is changed; in this case, chown performs the same function as chgrp. If only a colon is given, or if the entire operand is empty, neither the owner nor the group is changed.
The description from the manual pretty much sums it up. chown is used to change user-/group ownership of files. This is something that I absolutely love! Sometimes when I create packages using checkinstall, they are owned by the root account. To fix that graphically I’d have to open my file manager as root, find the file, get to the file properties and set the owner to be my user account. With chown, I can do all that in just a couple of seconds.
10. chmod
chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits.
chmod is used to set what you are allowed to do with a file, and who are allowed to do it. You can either define what changes to make to the current permissions, or you can define everything at once by entering a 3 digit number. Each number can be any number between 1-7. The first number in defines who can read the file, the second who can write to the file, and the third who can run the file. The higher the number, the more people are allowed to do whatever it is you’re defining. So for example, 777 would mean that anyone can do anything they want to it, and 000 means no one can do anything to the file. The other way of defining permissions is by defining the changes to make. That way is also made out of three digits. The first can be either u (user), g (group) or a (all). The next part is either a plus (+) or a minus (-). This defines wether to allow or disallow the action. The third part can be either r (read), w (write), or x (execute) (I’m not sure, but I think you can also use a to allow everything). So to allow the user to execute a file, we’d run:
chmod u+x file
It’s pretty simple once you get the hang of it.
Closing
That’s all for this post. I hope that you learned something from reading this, or that it at least made you a little curious about the command line (if you weren’t already). If you feel like I’ve missed a great command that I should have included, please let me know in the comments. Also, if I have completely misunderstood what some command does or if my explanation was kind of wonky, leave a comment explaining what I got wrong.
By the end of this week I’ll be posting the next part in the series “Console Emulation in Linux”. This time I think we’re going to be talking about Playstation, a couple of Sega consoles, and whatever other consoles that you want to read about. If you really want to read about a certain console, please leave a comment on the first post in the series, letting me know what you want to read about next.


There are 2 comments on this article: