Basic System Commands
From Linux 101, The beginner's guide to all things Linux.
The information contained in this article is wonderful general knowledge. You should know most of this regardless of operating system, simply because it is used on so many. This is mostly information you would not know coming from a GUI environment.
Contents |
[edit] Case Sensitivity
The Linux filesystems are all case sensitive. that means you could have 3 files in your home directory all called the same thing, but with different cases. For example, USR, Usr, and usr are all different to the Linux filesystem. So keep that in mind when you are looking at files. Most people will always just want to use lower case to simplify this.
[edit] Relative Directory Identifiers
There are two really useful "special directories" in each directory that are known as relative directory identifiers.
The ../ directory is the directory above the current one. This is called the parent directory. It exists in every directory in the filesystem. The root directory's parent directory is itself.
The ./ directory refers to the current working directory, the one you are actually in. You can find out the name of the current working directory with the pwd command.
In addition to these special directories, the tilde (~) is a shorthand form of refering to your home directory that is expanded by many shells. For example, ~/examplefile means in your home directory, the file named examplefile. Beginning any directory or file with the tilde implies it is in your home directory.
-
cp -R /mnt/cdrom ~/cdromcopy
will copy all files off the cdrom and put them in your home directory under cdromcopy.
[edit] Hidden Directories and Files
There is no genuine way to hide a file or directory on the system. However, there is a way to denote them as "less important" for a directory listing. If you begin a file or directory with a period, then it denotes it as such.
Since your user is restricted to writing long-term files to only your home directory, any program settings and the like are usually stored in files or directories beginning with a period. Without this notation, you would always be sorting through your personal files and your program's configuration files when listing directories. This would become tedious; thus, to allieviate this problem, the period notation was introduced.
[edit] Listing Directories and Files
In Linux the ls command is used for Listing Structures on the filesystem in the current directory.
-
ls
To show the hidden files introducted in the previous section, use the -a option. (a for all files!)
-
ls -a
To get more information about each file, use the -l option. (l for long form!)
-
ls -l
And, of course you can combine these to see all files and information about them.
-
ls -la
[edit] Basic Directory Commands
To move around the directories, use the cd command. (cd for change directory!) Some examples:
-
cd /usr/src
- will move you into the /usr/src directory
-
cd ..
- will move you into the parent directory, /usr
-
cd
- will move you into your home directory (if no directory name is given)
To make a new subdirectory under the directory you're currently in, use
-
mkdir <newdirectoryname>
[edit] Copying or moving files or directories
Moving a directory or file is accomplished by:
-
mv <source> <destination>
It is worth noting that though there is no "rename" command, mv implements this functionality for files and directories. For example:
-
mv foo bar
This will rename the file foo to have the new name bar.
The file and directory copying command is cp. Copying a directory requires the -r option, which tells cp to copy recursively from the directory:
-
cp <filesource> <destination> -
cp -r <dirsource> <destination>
[edit] Removing files or directories
The rm command removes files, and the rmdir command removes directories. You can use it as:
-
rm file1 file2 file3...
for as many files as you want, or
-
rmdir dir1 dir2 dir3...
for as many directories as you want. rmdir will only remove empty directories.
Because of the power of rm, I suggest you read up on it with man rm and completely grasp its usage. Remember once you remove files, they are gone. There is no safety net, recycle bin, or what have you. Data recovery is possible but very involved and commonly unsuccessful. Use it cautiously so you do not delete your work.
[edit] Notes Regarding Filesystem Navigation
Directories can be referred to with or without a trailing / character. Thus, cd .. has the same effect as cd ../ - both change to the parent directory. However, if you are in a directory that has both a file called foo and a directory called foo, then you must refer to the directory with a trailing / character to differentiate it from the file foo.
It is very common practice to see something like cd ../../../newdir which means move up 3 directories, then into that directory's newdir.
[edit] Disk Space
There are some simple commands that allow you to view disk usage statistics. df will allow you to see used and free space information about mounted filesystems.
-
df [options] [file...]
Note that the -h option is very useful since df will display the sizes in a more "human readable" form.
[example]$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda1 8772744 1991736 6335324 24% / /dev/hdb1 78786704 9513032 65271512 13% /home [example]$ df -h Filesystem Size Used Avail Use% Mounted on /dev/hda1 8.4G 1.9G 6.1G 24% / /dev/hdb1 76G 9.0G 63G 13% /home
For information about the amount of disk space that a specific file or directory is using, there is du.
-
du [options] [file...]

