banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Windows Migration: The Terminal-- Some Common Commads

by Peter Kelly (critter)

The commands

To start you off on your journey of discovery into the world of the Linux command line, I describe below some of the most common commands that I use. There are so many commands available in the Linux toolbox that every user will have their own set of most-used commands. Here are fifty of mine:

apropos, cal, cat, cd, chmod, chown, clear, cp, cut, date, df, dmesg, du, exit, file, find, free, grep, gzip, head, info, kill, less, ln, locate, lp, ls, man, mkdir, mount, mv, nano, passwd, ps, pwd, rm, rmdir, shutdown, sort, su, tac, tail, tar, top, touch, tr, umount, uname, whatis, whereis.

That's it, not that many, really. These are not necessarily the most used commands in Linux but if you can use these then you should have no problems with the others. Get used to using these few commands and you will feel a lot more comfortable using a terminal. Nine of the commands we have already met, but I will repeat them for reference.

Getting Help

When starting to use the terminal, the one thing that you need is help. These next commands will get you that help.

Most commands come with a user manual which can be accessed with the command

man command-name e.g. man ls (MS = help or command /?)

Navigate with the up, down, page up and page down keys. Get out by pressing the q key. If you need more information than you get in the manual, then try the commands handbook which you get to see with the command

info command-name

navigation is the same as for the man command.

When you don't know the name of a command, try the apropos command with some relevant text

apropos cdrom will list all commands it knows of that have cdrom in their description.

If you know the name of a command, but are unsure what it does try

whatis command-name to get a short description.

Should you need to know where a command and all of its its associated files are stored, then

whereis command-name will get you that information.

File Handling

As an ordinary user you have permissions to enter and list the contents of most directories, but not to change or delete anything except for those sub-directories in your home directory. To change to another directory, use the command

cd directory-name (MS = cd) for example cd /etc would put you in the /etc directory. cd on its own will take you back to your home directory. To change to your Documents directory, you could type

cd /home/linux_user/Documents or use the shorthand cd ~/Documents. Remember the squiggle?

To make a copy of a file, use the cp (MS = copy)command

cp filename destination the destination may include the directories describing the path to the copy of the file but those directories must already exist, and you must have write permission for at least the final directory in the path.

To copy directories recursively, use cp -R (MS = xcopy)

To rename a file, we use the mv old-file-name new-file-name command (MS = rename or move). The new file name may optionally include the address of a directory to which we have write permissions. To delete a file, we use this command

rm file-name (MS = del or erase) Be careful of this one as there is no undelete command. The PCLinuxOS version of this command will prompt you for confirmation before sending the file to oblivion, but this is not always the case.

To make a new directory, you need to have write permission for the directory where you want to place it and you use the command

mkdir directory-name. (MS = md or mkdir)

You can remove an empty directory with the command

rmdir directory-name (MS = deltree)

To remove a directory and all the files that it contains you will need to use the rm command with the option -r. If there are many files in the directory then the prompting can become quite tedious, but you can turn it off with the -f (force) option.

Important Be very, very careful with this. It is ruthless, quick and permanent. A little trick I use is to replace rm -rf with ls -R to list out which files would be deleted. If that is what I intended, then I issue the rm -rf death warrant. The -R option recurses down through any sub-directories (make sure that you use uppercase -R as lower case -r will only reverse the listing.)

You can create a new, empty file with the command

touch file-name

Often you need to refer to a file that is buried deep in a sub-directory and an easy way to do that, as in Windows, is to create a shortcut to it. In Linux there are two types of shortcut and they are known as links. There are hard links and soft or symbolic links. For now, we will concentrate only on soft links. The command to create them is

ln or ln -s for a soft link (MS = assign).

ln -s /home/linux_user/Documents/invoices/November/Acme_inc.txt acme_11 will create a shortcut named acme_11 which will point to the buried file making it easier to load into a text editor for example. This is very useful for editing things like system scripts without having to remember where they are.

Changing the permissions of a file requires the use of the chmod (change mode) command (MS = attrib). This command accepts several ways of specifying the new permissions and this can, at first, be quite confusing. To keep it simple, I use the following method.

There are three groups of permissions: owner, group and everybody else.
There are also three types of permission: read, write and execute.
Read =4, Write = 2 and execute = 1.
Add them up for each group of permissions like this
owner read + write = 4 +2 =6
group read = 4
everybody else no access = 0
chmod 640 myfile done!


Information

There are many ways to get information from the system and these next commands will give you most of what you need.

ls (MS = dir) as we have seen will list the contents of a directory.

pwd (MS = chdir) print working directory. This confirms where you are with the file system.

date (MS = date or time) This command typed on its own will print out the current date and time. This command can also be used to set the system date and time, but this is done so much better by the PCLinuxOS control center.

When you need to refer to a calendar in a hurry, just type

cal and you will get a nicely formatted calendar for the current month. cal -3 adds the previous and next months, cal -y gives a full year and cal 1066 will give historians a calendar for the year of the battle of Hastings.

Linux keeps a database of most of its system files. To find the location of a system file, use the

locate file-name command. A file that often needs to be edited is called fstab and is usually found in the /etc directory. When you can't remember where it is, use locate fstab and it will tell you.

You may sometimes be asked what kernel the operating system that you are logged into is using, and what is the architecture of the machine (32bit, 64bit etc.).

uname -a gives all of that, the -a means all, other options restrict the output.

To discover how much memory you have and how much is being used, use this command.

free (MS = mem) use the -m and -g options to display the results in MB and GB respectively. Linux doesn't use filename extensions to identify the type of data a file contains, sure, they are often used, but that is mostly for user convenience. The file command will tell you what type of data is in a file



The find command is one of those commands that is at the same time immensely powerful, indispensable and annoyingly complex to new command line users. Its syntax is slightly different from most Linux commands, but its use can be simplified to provide a more useful, basic utility. The syntax of the command looks like this:

find where-to-look options file-criteria-to-look-for what-to-match

Where-to-look is the directory in which to start the search

Options include things like -maxdepth 1 to restrict the search to the top level directory specified.

File-criteria-to-look-for This can be the name of the file, type of file e.g. directory, modification time, size, permissions or almost anything else that describes a file. You may also specify multiple criteria to fine tune the search.

What-to-match This can be the filename or a part of it including wild cards such as * (also regular expressions for those that understand such things). It can be the time since modification or access and it can be a minimum or maximum file size.

To search your home directory and directories immediately below but no further for files that end in .jpg you can issue the command

find ~ -maxdepth 2 -name "*.jpg"

To also find files with the .JPG extension use the case insensitive search predicate -iname in place of -name.

find ~/Documents/ -mtime 0 will list all files that have been modified in the last 24 hours. find / -size +1000M shows all files on the system larger than 1000MB (1GB) a minus sign would show all files under 1000MB – that's a lot of files! If you try and execute this without root privileges you will generate a lot of "permission denied" type error messages as the command attempts to enter directories to which you have no access granted. You can get around this by using a technique, discussed in the second of the articles, called redirection.

We send all error messages to a black hole called /dev/null

find / -size +100M 2>/dev/null

You'll want to keep an eye on your disk drives, because when they start to get full you will have problems. The simple

df (MS = net share, not really but the closest I could find) command will show you your disk free space.



To discover just how that space is being used, issue the following disk usage command du with no arguments. It will show what is being used by the files and directories in the current directory. Use du Documents/ to get results from that directory. Add the option -s to get only a summary. If you try it with the root directory du / then you will get a lot of errors as with the find command.

If you press the escape key whilst booting, you will see a lot of information scrolling past as the kernel sets things up. This output continues after boot up as the kernel starts and stops things or has anything to say that it considers important. This information can be useful when things are not working as they should, perhaps a usb drive is not being recognized. The dmesg command will print out this information but it prints out far more than you need and you will probably only be interested in the last few lines. Here we can use another filter command to control the output

tail will, by default, show only the last 10 lines of the file. tail -n 20 will give you twenty lines. To use the filter we need to use the pipe character again.

dmesg | tail -n 15

Similarly, if you want see only part of the beginning of a file, use the command head.

dmesg | head -n 20

To display the contents of a file from beginning to end, we have the cat command which we have already met and, just as tail is complemented with head, the cat command has its own counterpart tac, which displays the lines of text in the file in reverse order, last line first. Why on earth would you want to do that? Well, many Linux commands produce log files documenting their experiences and output. Using the tac command allows us to see the latest output from the command first.


System administration

To control ownership of a file, we have the chown command which allows us to change both user and group ownership of a file. The -R option allows you to recurse down into directories, changing the ownership of all files therein.

chown user:group file

chown user file

chown :group file

You may specify just the user, both user and group separated by a colon or just the group which must then be preceded by a colon.

An ordinary user can change the group ownership of a file to any group of which they are a member, but root or super-user privileges are required to change user or global group ownership.

Some distributions use the sudo command to grant temporary root powers. For security reasons PCLinuxOS does not. This distribution uses the su command (switch user).

The su command defaults to switching to the root user unless another user name is specified. You can switch to assume the identity of any user whose password you know (which is why you should keep your password secret.)

su guest would prompt for the password of the user guest, and then allow you to do what you will with guest’s files and directories, including deleting them!

For this reason su should be used with caution. As user root you have the power to do anything with any or all files on the system!

There are a couple of options to su that you should be aware of.

With no options su grants you the powers of root but leaves you where you were in the file system.

The command su -l gives you a login shell. It is just as if you had logged in as root and the squiggle after the prompt now means the directory /root, not your own home directory. To relinquish the powers of root and return to being a mere mortal, type control-d or the command exit (MS = exit).

The command su -c command will prompt you for the root password, execute the command as root and then return you your normal user status.

To change your password use the passwd command. It will prompt you for the current password, the new password and then confirmation of the new password. If these last two match, then the password will be changed. With root permissions, you may also specify a username for whom you want to change the password (or suspend to prevent the user logging in).

In order to correctly power off or reboot a system from the command line, root has the command shutdown (MS = reboot).

On a multi-user system, you would want to warn users and give them time to save their work before performing such an operation, and this is what this command does. The -r option tells the command to reboot the sysyem and the -h command tells it to halt the system. There is a mandatory time option which for a single user system may be now. If you don't specify a warning message, then a default message is issued.

shutdown -r now Reboot the computer immediately.

shutdown -h 18:30 "The system will be halted at 6:30pm for routine maintenance"

shutdown -c Abort a scheduled shutdown.

Every time that you execute a command or run an application, the kernel will start a separate process for that command. Often that command will generate many sub-processes. Occasionally a process may 'hang' or go into an infinite loop, refusing to respond to your efforts to terminate it. Here we need to know how the system refers to that process, so we have to find its process id. For this, we have the command ps (MS = tasklist). There are many options to this command, but ps -u will list processes owned by you. If that doesn't show the process, then ps ax usually will, but the output will be quite lengthy. Look for the number under the PID column.

To terminate the process we use the command kill PID. Sometimes a process will resist this attempt and then we have to use the much stronger kill -9 PID. Look at the following example.



Firefox is running in process 13111 until the kill command is issued which terminates the application.

Another command that allows you to work with processes is top (MS = mem). Here Firefox has the process id 13928.



To terminate the process press k and you will be prompted PID to kill:

enter 13928 and you will then be prompted kill PID 13928 with signal [15]: Type y to end the process. The top command can be quite complex at times but there is an alternative which is much more user friendly. It is called htop and is not installed by default but is available for installation from the repositories using the package manager Synaptic. I would recommend installing htop.

Unlike Windows, Linux does not use drive letters like c:. Each partition is mounted at a particular place within the file system and the files on the device are then accessed from that place. Usually all of this is handled seamlessly by the text file /etc/fstab, and you don't need to bother about it. If you insert a usb drive, then this is unlikely to be listed in fstab. Modern systems usually detect this and automatically mount (or offer to mount) the device.

The command line allows you to have more control over this with two commands named mount and umount (no, that is not a typo. The command is umount not unmount: I told you Linux users were lazy typists.). To use the command you need to know how the kernel refers to the device. This is usually with a device name like /dev/sda1 which is the first partition (1) on the first hard drive (a). if you insert a usb thumb drive and then issue the command dmesg | tail –n 20 then you will see some output like this (I added the underlining).

usb 5-2.1: ep 0x81 - rounding interval to 32768 microframes, ep desc says 0 microframes

usb 5-2.1: ep 0x2 - rounding interval to 32768 microframes, ep desc says 0 microframes

Initializing USB Mass Storage driver...

scsi8 : usb-storage 5-2.1:1.0

usbcore: registered new interface driver usb-storage

USB Mass Storage support registered.

usbcore: registered new interface driver uas

scsi 8:0:0:0: Direct-Access Kingston DataTraveler G3 PMAP PQ: 0 ANSI: 0 CCS

sd 8:0:0:0: Attached scsi generic sg4 type 0

sd 8:0:0:0: [sdd] 7653312 512-byte logical blocks: (3.91 GB/3.64 GiB)

sd 8:0:0:0: [sdd] Write Protect is off

sd 8:0:0:0: [sdd] Mode Sense: 23 00 00 00

sd 8:0:0:0: [sdd] No Caching mode page present

sd 8:0:0:0: [sdd] Assuming drive cache: write through

sd 8:0:0:0: [sdd] No Caching mode page present

sd 8:0:0:0: [sdd] Assuming drive cache: write through

sdd: sdd1

sd 8:0:0:0: [sdd] No Caching mode page present

sd 8:0:0:0: [sdd] Assuming drive cache: write through

sd 8:0:0:0: [sdd] Attached SCSI removable disk

Line 8 (underlined) shows that the system has found my thumb drive, a Kingston Data Traveller. Now look at line 17 (also underlined). This tells me that the kernel has named this device /dev/sdd and there is one partition on it named /dev/sdd1. This is the information that I need.

I need to mount this device somewhere so that I can access it and the system has a special place for removable devices - /mnt. If I mount it there then I have nowhere to mount further devices so I need to create a directory inside (or under) this directory. To do this I need root privileges.

su (enter the root password)

mkdir /mnt/usb1 and then, finally the mount command

mount /dev/sdd1 /mnt/usb1 read this as "mount this_device in this_place".

ls /mnt/usb1 would list the contents of the thumb drive to the screen.

The Linux system writes data asynchronously. This means that the data is not written immediately to the device and so it is very important the device is correctly removed or data loss may occur. This is done with the umount command which synchronizes data writes before unmounting the device.

Umount /dev/sdd1 The device may now be safely removed. Controlling command output

When you receive some output from a command it invariably is not in the order that you want it. To sort it into the required order we use a command called, naturally, sort (MS = sort)

There are many options for sort to produce data sorted alphabetically, numerically, reversed, by date etc. This is is one of many Linux tools known as a filter. Data is passed to a filter and the output is the altered data. This is one of the basics of command line operation and part of the real power of Linux. The usual way of using a filter is to separate the command producing the data from the filter with the vertical bar character known as a pipe.

The tr command, while not used very often, is designed to translate one set of characters into another. One use might be to translate lower case to upper case. The command is used as a filter and I find the -s option most useful. This option squeezes out multiple repeats of a listed character and replaces them with a single instance of that character. For example cat textfile | tr -s " "

The cat command sends the file to the tr command which then removes all multiple spaces. Use "\t" to remove extraneous tabs. This doesn't actually alter the original file, only the output from the command is affected as it passes through the filter.

Some commands produce so much output that it is difficult to find the information that you want. You could pipe the output to a pager such as less (MS = more), but you would still have to search through a lot of irrelevant stuff. Linux has a command named grep (the name is actually an acronym but that's irrelevant here) that we can use to solve this little problem. The grep (MS = find) command is designed to search inside a file for text that matches a pattern.

grep "Once upon a time" ~/Documents/* would search all of the files in your Documents directory for the given phrase.

If we pass the output of a command togrep then it will search that output for the expression.

ps ax | grep firefox will make it a lot easier to find the process id of the unresponsive firefox.

The -v option inverts the match and displays lines that do not match.

A lot of Linux commands produce columns of information separated by spaces, tabs, commas or colons. Most of this data you will not be interested in, so wouldn't be nice if you could cut out and display only the relevant data? Well you can with the cut command, which sees the data as strings or fields of characters split by a separating character or delimiter (tab by default). The output from the df command with no options would look something like this

Filesystem    Size    Used    Avail    Use%    Mounted on
/dev/sda1     26G     8.4G    16G      35%        /
/dev/sda5     352G    149G    186G     45%        /home
/dev/sdb6     391G    247G    124G     67%        /share
none          7.8G    124M    7.7G      2%        /tmp
/dev/sdd1     3.7G    613M    3.1G     17%        /mnt/usb

These fields appear to be delimited with a tab character, which is the default for the cut command. If we want only the Filesystem and Mounted columns, then we can ask cut for fields 1 and 6 using the -f option to specify the fields.

ps | cut -f 1,6 Unfortunately this will not work in this case as the delimiter is not a tab as we thought, but the output has been prettied up by padding with empty spaces. We can use the tr -s command to squeeze out the extra spaces and then use the -f option with cut to set the delimiter.

df | tr -s ' ' | cut -d' ' -f 1,6

Filesystem Mounted
/dev/sda1 /
/dev/sda5 /home
/dev/sdb6 /share
none /tmp
/dev/sdd1 /mnt/usb

Miscellaneous commands

Sometimes the terminal screen can get cluttered and distract from the job at hand.

Issue the clear command and you can start over with a blank page. (MS = cls)

No list of terminal commands would be complete without a text editor. Text files are so central to the operation of Linux that there are hundreds of commands to manipulate them. This text has to be created and often edited. There are many text editors available in Linux including the extremely powerful but dauntingly unfriendly vi. PCLinuxOS comes with a very nice, simple command line text editor called nano (MS = edit). If not already installed in your version then it is worth opening Synaptic and adding it to the system.

Type nano filename and if the file exists, it will be loaded. If not, then a blank file will be opened ready to save when you have added some content. At the bottom of the screen is a menu, ^ means hold down the control key and then press the letter following it to perform the associated operation. Control-g will get you some basic help.

If you want a hard copy of a file, and you have a printer installed, then lp (MS = print) myfile will print it out. It's that simple.

Most Windows users are familiar with the commercial file compression programs Winzip or pkzip. The Linux command line equivalent is gzip

gzip myfile compresses a file with the default options and the default file extension .gz

gzip -d myfile.gz decompresses a file with the default options

gzip also accepts a number between 1 and 9 as an option, -1 for faster compression, -9 for more aggressive compression.

tar is a command from the past but it is still very useful. The letters tar are an acronym for tape archive. When files were traditionally written to magnetic tape for backup, collections of files were bundled into an archive for later retrieval. This is still useful today although the files are more usually 'tarred' and compressed then the 'tarball' is transferred to another file system or over the internet.

The default file extension (to aid humans) is .tar. The tar command can now automatically compress files using several compression techniques. This is usually denoted by a file extension of .tar.gz or similar.

tar -cf file1 file2 creates a tarball of files file1, file2.

tar -czf docs.tar.gz ~/Documents/ creates a zipped file of all the files in your Documents directory.

tar -tf docs.tar.gz lists the files in the archive.

tar -xzf docs.tar.gz extracts the files from the archive.

There are so many options to this command that you really do need to either read the documentation or restrict your usage to these simple examples, which will probably be all that you ever need.



Previous Page              Top              Next Page
Copyright (c) 2013, The PCLinuxOS Magazine. All Rights Reserved.