basic commands

I originally wrote this tutorial when i first started using linux, at the my debuts I suffered and so I decided to help myself and others by writing a little howto for the first (or almost first) steps with GNU/Linux. I started by writing some notes about how to get around the filesystem, copy files, to know how much space is occupied by what folder etc etc... There might even be a little option that an experienced user might not know... :p

commands used :

  • ls : list
  • pwd : tell me where i am (print working directory)
  • cd : change directory
  • cp : copy
  • mv : move
  • rm : delete
  • df : show disk space usage (disk free)
  • du : disk usage

ls - list

ls is for listing files. by default this will list the files in the directory where you are, for example :

[user@machine]$ ls images/ text.txt

You can also specify the directory for which you want to list it's contents, for that all you need to do is specify the path to that directory after the command ls, some call it an "argument", i wont argue with that and hence do the same.. : ] example :

[user@machine]$ ls /home/user images/ text.txt

This command will list the contents of the folder /home/user no matter where it's executed, the same logic can be applied to most of the commands used in this tutorial.

Another feature are options, they are often letters prefixed by a minus sign, for the command ls among all the available options I'll just show a few that I believe are most useful for beginners, to grasp the idea best is to try

  • -l : show details; permissions, size, etc...
  • -a : show all files (all = even the "hidden files"
  • -h : human readable; when this is used with "-l" the file size becomes, as said, readable by humans (Ko,Mo,Go)
  • -S : sort by size

of course these options can be combined for example ls -lha will list all files and their sizes in a human readable format.

pwd - print working directory

i haven't explained how to move around the file system, but first it's good to know where you are right ? the command pwd shows where you are with the absolute path, meaning it starts from the "slash" "/" which represents the absolute top of the file system and all the way down to the directory where you are. example ? mkay.

[user@machine]$ pwd /home/user/folder

cd - change directory

now we can think about moving about, for example there is a directory images in /home/user, to go there you can execute :

[user@machine]$ cd /home/user/images

this will take you to the directory images no matter where you are when you execute it, this is because it is the absolute path. you can also use relative paths, meaning the starting point is the directory where you are. if you are in the directory /home/user you can simply type cd images to go to /home/user/images.

maybe you have noticed, when you execute ls -a there is a "." and a "..". the . represents the current directory and the .. represents (yo yo) the parent directory.. hence, to go up you can type cd .. (2 dots) and cd . (1 dot) .. . well that just leaves you in the current directory, i'll admit in this case it is not so useful, but later on in life it will be.. .

so, an example; if you are in the directory /home/user the two following commands will take you to the SAME directory :

cd ../other_user/images2 cd /home/other_user/images2

another little tip, cd ~ will take you to your home, actually the ~ represents your home folder. .. Tip: you can just type cd and you will be taken to your home directory.

cp - copy

to copy a file all you need to do is execute cp source destination so: cp text.txt images/text.txt will copy 'text.txt' to the directory 'images', if you don't intend to change the filename (text.txt) you can just type : cp text.txt images/ which will copy text.txt in the directory images.

of course you can use absolute and or relative paths for the source and/or destination.

among many options, i like these :

  • -r : copies recursively, useful for copying directories
  • -p : preserve attributes such as date and permissions

mv - move

for moving a whole folder or just files mv does the job, you can also rename a file by "moving" it. mv /path/file /other/path/ will move the file to /other/path/file mv file newfile will rename file to newfile mv directory/ /other/path/ will move directory to /other/path/directory

rm - remove

now you can start erasing things. .. be careful there is no going back (or at least, no easy way)

some useful options are :

  • -r : recursive; useful for erasing a folder AND all it's contents
  • -f : forece; sometimes you system will ask you "are you sure blah blah", this option is the "just do it" mode

examples :

rm file #erases a file (called "file") rm -rf /home/user/images/ # erases the directory "images" and all it's contents

df - disk free

when you need to know how much space you have to spare on your disk drive df is handy, actually it will tell you how much space you have, total, used, free per partition on whatever is mounted.

Options I like:

  • -h : human readable
  • -T : show the file system types

du - disk usage

very useful for getting the space used by a directory, du lists the contents of every present directory and goes on recursively to list everything beneath showing the size of every file and then the total.

this is best used with such options as :

  • -h : human readable
  • -s : shows only the total

a combination i enjoy is : du -hs */ this command prints the size of each present folder one by one .. .


du . # here the . is used to indicate that we want to start measuring from the present folder. du -hs . # does the same as the above except that the result is human readable and shows only the total. du -hs /home/*/ # useful when you want to know the size of your user's home directories.

depending on your distribution some aliases might be preconfigured, for example with mandrake 9.1 (yes that was a while ago) df was aliased to df -h, you can check and or configure such things in /etc/profile.d/

learn how to fish and you'll never be hungry

this means, there is something called the "manual" for almost every linux command (and program), you can use it by executing man command and that will print out the manual

man pwd

now you know everything, you can start helping out others.. . :]