Contents
- Introduction
- Documentation
- Browsing Filesystem
- Files and directories movements
- Creation and Deletion
- Shortcut/Symlink
- Print file content
- Edit file content
- Find files
- Hard drive disk usage
- Memory usage
- Processes management
- Permissions management
- Download
- Extraction
- Mount
- Package management
- Keyboard shortcuts
- Useful links
Introduction
The command line (or "terminal" or "command prompt") is a window where you can type commands. In gNewSense you can start using the command line by clicking Applications -> Accessories -> Terminal. You may also use the command line if you choose to run your computer without starting the window manager.
GNU Bash is the default language interpreter for gNewSense and many other GNU/Linux distributions. An understanding of it and how to program shell scripts is wise. Please see manual available online.
With all of the pretty graphics in GNU/Linux, you may wonder why people still use the command line. Well, for starters, it's fast! Installing new software, renaming a bunch of files, resizing all of the images in a directory, or even simply moving from directory to directory looking at your files, all of these can be done with a single command in a terminal window.
The command line is great for automation. Just type in what you want the computer to do, press enter, and go make a sandwich. For example, by using the command line, you can tell your computer to download a file, and when it finishes, shutdown. Then you can go to sleep, download the file, AND save electricity. Convenience!
And using the command line looks cool. I recommend changing the color settings of the terminal to be bright green text on a black background. Even if you have no idea what you are doing, the people who see you with that window open will look upon you with awe.
With practice you might decide that you prefer to do things using the command line instead of with the mouse. You may even find yourself saying, "Gee, that was so easy to do when I used the command line! Why can't things be this easy in other operating systems??"
Documentation
If you want to have information on the command mycommand, you may type:
man mycommand
This will provides you the mycommand definition and options. At the bottom are references to commands with a number, like in iwconfig(8). If you want to read this specific documentation, for this example, type:
man 8 iwconfig
Sometimes, especially for GNU software, more detailled documentation is available through info formatted files. For this kind of documentation, you should instead type:
info mycommand
If you just want to have a short list of most useful commands or when previous commands were unsuccessful, type:
mycommand --help
Browsing Filesystem
pwd
To know where you are in the directory structure, type:
pwd
ls
To list files and directories in the current directory:
ls
You can also use options:
ls -a # lists all files and folders including hidden ones, whose name begin with a point ls -l # lists files and folders with rights, size and other useful informations ls -hl # same as above with human readable sizes ls -R # lists files and folders recursively ls -sh # lists files and folders and prints their size in KB, MB, etc
ls command is particularly powerful with the use of grep and piping |:
ls -al | grep ".jpg" # lists all files and directories, even hidden, which contains .jpg (case sensitive) in their name ls | grep -i "gnu" # lists files and directories which contains GNU or gnu or GnU, etc (case insensitive) in their name
cd
To change directory:
cd mydestination cd .. # to go to the parent directory of current directory cd / # to go to filesystem base directory, which has no parent directory cd ~ # to go to your personal home folder, /home/myusername
Files and directories movements
cp
To copy a single file or directory:
cp myfile destination cp -R mydirectory destination # copies directory AND its content to destination cp -u myfile destination # updates destination content cp -v mydirectory destination # be verbose cp -R mydirectory/* destination # copies all the content of directory to destination cp -R .* destination # copies all the content of current directory, including hidden files, to destination
To copy several files or folders:
cp myfile1 myfile2 myfile3 destination
mv
To move a file or directory:
mv myfile destination mv -u myfile destination # updates destination content mv -v mydirectory destination # be verbose
Creation and Deletion
touch
To create a file:
touch myfile
mkdir
To create a directory:
mkdir mydirectory mkdir -p path/mydirectory # creates directories composing path if they don't already exist, then creates mydirectory
rm
Remove a file or folder:
rm file rm * # removes all files in current directory rm -R mydirectory # removes mydirectory rm -Rf mydirectory # forces mydirectory removal rm -i *.jpg # asks you for each file ending with .jpg if you want to remove it
rmdir
Remove an empty directory:
rmdir mydirectory
Shortcut/Symlink
To create a symbolic link (shortcut) to a file or folder:
ln -s path/myfile linkname
Print file content
cat
To print the entire content of a file directly in the command-line:
cat myfile
less
To see a file with less file reader:
less myfile
Press q to exit.
less let you browse the file from top to bottom, that is useful for big files, for which cat will only let you see the bottom.
To find "something" in a file you are reading with less, type:
/something
type n to find next occurence of "something".
You can also browse two files, one after the other:
less myfile1 myfile2
To read myfile2, type this when you have finished reading myfile1:
:n
Edit file content
There are three well known command-line editors: nano, Emacs and vi.nano is easier while Emacs and vi are more powerful.
Nano
To edit a file using nano, just do:
nano myfile
Save by pressing both ctrl and O keys.
Quit by pressing both ctrl and X keys.
GNU Emacs
To edit a file using GNU Emacs, do:
emacs myfile
Save by pressing ctrl-x then ctrl-s.
Quit (and save) by pressing ctrl-x then ctrl-c.
Find "something" by pressing ctrl-s, then type something. Press ctrl-s another time for next occurence.
Find "something" and replace it with "whatever" in the entire file by pressing ctrl-x, then type replace-regexp and press Return. Type something then press Return, type whatever then press Return.
Vi
To edit a file using vi, do:
vi myfile
To write something, you need to press i (for insert) key.
Save by pressing esc key, then type :w.
Quit by pressing esc key, then type :q.
Save and quit by pressing esc key, then type :wq.
Quit without saving by pressing esc key, then type :q!.
Find "something" by pressing esc key, then /something like with less command. Press return for next occurence.
Find "something" and replace it with "whatever" in the entire file by pressing esc key, then type s/something/whatever/g like with sed command.
Find files
To find files and folders on GNU/Linux, you can use either find or locate commands. The latter is faster but requires to build a database.
Find
To find files or folders which name contains "photo" (case sensitive), type:
find -name "*photo*" find -iname "*photo*" # for "photo", "PHOTO", "PhOtOs", etc (case insensitive) find -mtime -3 # for files and folders modified less than 3 days ago
Sometimes, you have no read access on directories being searched. In this case, the following command may be helpful:
find / -iname "libc*" 2>/dev/null
To combine criteria, use -a (AND) or -o (OR) inside brackets:
find . \( -iname "*photo*" -a -mtime -3 \)
You can also find files or folders that do not respect a criteria, by using ! character:
find . \( -iname "*photo*" -a ! -mtime -3 \)
You may want to execute one command on the file(s) or folder(s) found, -exec and ok (-exec with confirmation) fulfill this need:
find ! -mtime 1000 -ok mv {} trash \;For more complex commands, you may want to use xargs with a pipe, see http://en.wikipedia.org/wiki/Xargs.
Locate
Todo
Hard drive disk usage
df
To know how much space is available on "mounted" partitions:
df -h # -h for human-readable size format
du
To know the size of a directory:
du mydirectory # lists all the content of mydirectory and sums to obtain mydirectory content size du -h mydirectory # -h for human-readable size format du --max-depth=0 mydirectory # prints size of mydirectory
Memory usage
To know how much RAM memory is available/used/free on your system:
free free -m # displays memory in megabytes
Processes management
Ressource usage
top # lists most processor-consuming processes with their pid
Kill
To kill a process, you can either use kill or killall commands, the first uses process' id (or pid), the latter uses process' name:
kill 3548 # kills process for which pid is 3548 killall this-application # kills process named this-application
Sometime, the process won't be killed, in that case, the following option could be useful:
kill 3548 -s KILL # kills for sure! killall this-application -s KILL
Permissions management
Permissions on files and folders is what makes Unix systems more secure, althought this alone cannot be a "warranty".
To change owner of a file or folder:
chown me:mygroup myfile # change myfile's owner to "me" and its group to "mygroup" chown me:mygroup mydirectory # change mydirectory's owner to "me" and its group to "mygroup" chown me:mygroup mydirectory -R # change mydirectory's and its content's owner to "me" and their group to "mygroup", recursively
To change permissions modes on a file or folder:
chmod 400 myfile # rights become r-------- chmod 200 myfile # rights become -w------- chmod 100 myfile # rights become --x------ chmod 764 myfile # rights become rwxrw--r-- chmod u+x myfile # adds execute mode for myfile's owner user chmod g+x myfile # adds execute mode for myfile's owner group chmod o+x myfile # adds execute mode for myfile's others chmod +x myfile # adds execute mode for myfile's owner user, group and others chmod 700 mysecretdirectory -R # applies rwx------ rights to all content of mysecretdirectory, recursively
Download
To download the file at http://heanet.archive.gnewsense.org/gnewsense/cdimage/gnewsense-livecd-deltah-2.1.iso.torrent in command-line, do:
wget http://heanet.archive.gnewsense.org/gnewsense/cdimage/gnewsense-livecd-deltah-2.1.iso.torrent
To continue an interrupted download:
wget -c url-to-myfile
Extraction
To extract a compressed tar file (or tarball):
tar -xzf myfile.tar.gz # extract (-x) using gzip (-z) file (-f) "myfile.tar.gz" tar -xvzf myfile.tar.gz # -v: verbose tar -xjf myfile.tar.bz2 # extract using bzip2 (-j)
To extract a compressed gz file:
gunzip myfile.gz
To extract a compressed bz2 file:
bunzip myfile.bz2
Mount
To mount the partition /dev/hda2 on /mnt/hda2:
mount /dev/hda2 /mnt/hda2 mount /dev/hda2 /mnt/hda2 -t vfat # if filesystem of /dev/hda2 is fat32 mount myfile.iso /mnt/cdrom -t iso9660 -o loop # content of myfile.iso is then browsable in /mnt/cdrom
If you want to automatically mount some partitions at startup, you may be interested by /etc/fstab.
Package management
This section is specific to gNewSense and deb based GNU/Linux distributions.
Search a package
To look for a package in the distribution you are using:
apt-cache search package
Show details on a package
apt-cache show package
Manage packages
See also the page on apt-get and aptitude.
Available online
To install a package available for the distribution you are using:
apt-get install package apt-get install package1 package2 aptitude install package aptitude install package1 package2
To remove an installed program:
apt-get remove package apt-get remove package1 package2 aptitude remove package # removes also package's dependancies which are not necessary for others aptitude remove package1 package2
To synchronize your system's list of packages with server's list:
apt-get update aptitude update
To update packages you already installed on your system:
apt-get upgrade aptitude safe-upgrade
Available offline
To install a deb package you have downloaded:
dpkg -i package.deb
To remove a deb package you have installed:
dpkg -r package.deb
Keyboard shortcuts
There are some useful keyboard shortcuts in command-line:
ctrl-C # interrupts the program which is currently running ctrl-D # exits ctrl-L # cleans screen ctrl-shift-T # opens a new tab (in terminal emulators such as gnome-terminal)
Useful links
The FSF sells the book Introduction to the Command Line, you can also read the book online for free (as in "free beer"). Finally there is a summary of command-line commands.
