Basic Commands in Linux
From WikiGuruja
Contents |
Getting Help
man
man displays the manual page for the specified command
A useful option is:
-k TEXT search manual page titles and synopsis lines for TEXT
Examples:
To display the manual page for the chown command:
$ man chown
man has different sections.
- section 1 is user commands
- section 2 is system calls (used by programs to communicate with the kernel)
- section 3 is library reference (for programming in C)
- section 4 is device drivers
- section 5 is configuration files and other file formats
- section 6 is games
- section 7 is miscellaneous (for example, "ascii" map and C "operator" precedence)
- section 8 is system commands (like user commands, but mostly for root)
A section number can be specified before the page name. For example, man chmod normally shows the user command "chmod". To see the system call "chmod":
$ man 2 chmod
To search the man pages for "newsgroups",
$ man -k newsgroups actsync (8) - synchronize newsgroupsoups newsgroups (1) - a program to list unsubscribed newsgroups
- If this does not work you may have to run the makewhatis command.
info
info is an advanced man command that is sometimes available. It displays the improved manual pages in Info format for specified command.
Examples:
To display the manual page for the grep command:
$ info grep
To find occurrence of 'grep' in all info manual pages:
$ info --apropos grep "(autoconf-2.13)Examining Declarations" -- EGREP_CPP "(autoconf-2.13)Examining Declarations" -- EGREP_HEADER "(autoconf-2.13)Old Macro Names" -- HEADER_EGREP ...
To see the physical location of 'grep' info manual page:
$ info -w grep /usr/share/info/grep.info.gz
To view a file a info page:
$ info -f ./some_cmd.info.gz
apropos
apropos searches the manual page short descriptions for a specified keyword
On many systems this is exactly the same as the -k option of the man command.
Examples:
$ apropos newsgroups active (5) - list of active Usenet newsgroups newsgroups (1) - a program to list unsubscribed newsgroups
whatis
whatis displays short man page descriptions. Unlike
Examples:
$ whatis info info (1) - read Info documents info (5) - readable online documentation
$ whatis chmod chmod (1) - change file modes chmod, fchmod (2) - change mode of file
makewhatis
makewhatis creates the database for the whatis, appropos, and man -k commands. This is commonly run automatically by your system however sometimes you need to run this manually.
Examples:
# makewhatis
$ sudo makewhatis
File System Utilities
ls
ls is a utility for listing the files in a directory.
Most used options are:
- -a
- all files (include files with . prefix)
- -l
- long detail (provide file statistics)
- -t
- order by creation time
- -r
- reverse order
- -F
- mark directories with /, executables with *, symbolic links with @, local sockets with =, named pipes (FIFOs) with |
Other options include:
- -Fx --color
- color-coded listing
- -s
- show filesizes
- -s -h
- show filesizes in kilobytes and megabytes
ls *st* : list files that contain st in name ls > list : output list to file named "list" ls | more : fit listing to screen
Examples
$ ls fish hello.txt
$ ls -l -rw-r--r-- 1 username groupname 0 Apr 11 00:09 fish -rw-r--r-- 1 username groupname 11 Apr 11 00:10 hello.txt
Several systems have an alias ll which does the same as ls -l:
$ ll -rw-r--r-- 1 username groupname 0 Apr 11 00:09 fish -rw-r--r-- 1 username groupname 11 Apr 11 00:10 hello.txt
Be careful with the -F option. Here is one example:
$ ls -F /usr/X11R6/bin/X* /usr/X11R6/bin/X@ /usr/X11R6/bin/Xnest* /usr/X11R6/bin/Xprt* /usr/X11R6/bin/Xmark* /usr/X11R6/bin/Xorg* /usr/X11R6/bin/Xvfb*
We do not know yet if there is a symbolic link "X" and an executable "Xmark" or if "X@" and "Xmark*" are just the names of normal files. (Though "@" and "*" are not much found in filenames, they are possible.) So we check by dropping the -F:
$ ls /usr/X11R6/bin/X* /usr/X11R6/bin/X /usr/X11R6/bin/Xnest /usr/X11R6/bin/Xprt /usr/X11R6/bin/Xmark /usr/X11R6/bin/Xorg /usr/X11R6/bin/Xvfb
mkdir
mkdir is a utility for creating a directory.
Examples
$ mkdir newdirectoryname
fdisk
fdisk, cfdisk and sfdisk are utilities for partition manipulation. cfdisk has a good menu options.
Examples
$ cfdisk -z /dev/hda $ fdisk /dev/sda
| |
cd
cd changes the current directory of the shell. This current directory will be used by other programs launched from the shell.
Because "cd" changes the state of the shell, it is a shell builting command. It contrast, most commands are separate programs which the shell starts.
Examples
Change to 'foobar' directory:
$ cd foobar
Change to you home directory, cd command used without an option will drop you back into you home directory.
$ cd
~ (tilde) stores the path to your home directory, this command has same effect as the previous one.
$ cd ~
Tips:
- The "CDPATH" might only work in some shells. For example, ksh has it.
By setting "CDPATH" environment variable in your shell you can take advantage of shell command completion facility.
$ echo $CDPATH .:/usr/local:/usr/share/doc
If you have the $CDPATH set, then you press 'TAB' key and get possible path completions
$ cd bas [TAB] base-config/ base-files/ base-passwd/ bash/ bastille/
pwd
pwd (for Print Working Directory) shows the current directory that you are in.
Though "pwd" is often available as an external program (like /bin/pwd), many shells offer an equivalent version as a shell builtin command. Like any external command, "pwd" would inherit the current directory from the shell or other program that starts it.
Examples
$ pwd /home/username
You can change the directory, you can also
$ cd /usr $ pwd /usr
You can also use "pwd" in scripts. If you have enough experience with scripting, then you would know that the next line complains if the current directory is /home/username.
$ test "x$(pwd)" = x/home/username && echo wrong directory
chroot
chroot changes the root filesystem. The "chroot" page at the Linux questions wiki explains why you might want to do this.
Examples
To change the root filesystem so /mnt/usbdrive/ becomes / and files outside of it cannot be seen:
# chroot /mnt/usbdrive/
You must be root user to "chroot". Other users would be able to use "chroot" to gain root (superuser) priveleges, so their use of "chroot" is disallowed.
$ chroot /mnt/usbdrive/ chroot: /mnt/usbdrive/: Operation not permitted
cp
cp copies a file
Examples
$ cp -i debian Debian
Copy all files from the current directory to a target directory:
Syntax:
$ cp -r -t <target> <source>
Example:
$ cp -r -t /opt/cwk/mysqldata/ .
mv
You can use the command mv to move or rename files and directoris.
Examples
Rename file 'unix' to 'Unix' $ mv -i unix Unix
Moving file Unix from your home directory to /tmp. $ mv -i ~/Unix /tmp/Unix
The -i indicates that the command will be interactive. It prompts before overwrite.
To move and rename a directory to another place use the following command:
$ mv <source> <target> $ mv mediawiki-1.6.7 /opt/www/wikilinux
rm
rm deletes a file from the filesystem, like the "del" command in DOS.
The GNU long options (like --directory) are available on Linux, but not most other systems.
Some useful options are:
- -d, --directory
- unlink FILE, even if it is an empty directory (some systems let superuser unlink non-empty directories too)
- -f, --force
- ignore nonexistent files, never prompt
- -i, --interactive
- prompt before any removal
- -P
- (*BSD only) overwrite file before deletion
- -r, -R, --recursive
- remove the contents of directories recursively
- -v, --verbose
- (GNU only) explain what is being done
- --help
- (GNU only) display help and exit
- --version
- (GNU only) output version information and exit
Examples:
The usage of "rm" is considered potentially more dangerous than equivalents in other operating systems because of the way the shell parses wildcards and names of special directories and in its non-verbose actions.
Here is a classic example. Instead of deleting files that end with .o ("*.o") it deletes all files in the directory ("*") and also a file called .o. There is an unwanted space between the asterisk and the period.
$ rm * .o rm: cannot remove `.o': No such file or directory
To remove a file whose name starts with a `-', for example `-foo', use one of these commands:
$ rm -- -foo
$ rm ./-foo
To remove a directory:
$ rm -r <directory>
It might be useful to create an alias such as "remove" which moves the files to a local "trash" file so you can go there and recover files you accidentally "remove"d.
Secure deletion of files:
Note that if you use rm to remove a file, it is usually possible to recover the contents of that file since rm does not remove it from the hard disk. It simply removes the filesystems link to it.
On *BSD systems, the -P option overwrites the data with the file before removing it.
$ rm -P secretfile
However, as the NetBSD manual page explains it:
- Recent research indicates that as many as 35 overwrite passes with carefully chosen data patterns may be necessary to actually prevent recovery of data from a magnetic disk. Thus the -P option is likely both insufficient for its design purpose and far too costly for default operation.
So while examining the data (using fsdb or making a disk image) will not reveal the secret data, other methods (such as laboratory examination of the disk) will reveal the data. In short, rm -P does not delete data securely. A program that attempts to delete data securely is GNU shred, available on Linux. But "shred" is not always successful in secure deletion; read its entry below.
rmdir
rmdir is a utility for deleting directories.
Examples
$ rmdir directoryname
shred
shred overwrites a file multiple times with special data patterns to make the old contents of the file unrecoverable from a disk, especially a hard disk. This command is part of GNU coreutils, so it is often only available on Linux systems.
Note that this actually is ineffective on most filesystems because they can keep old copies of data. Most popular Linux filesystems (including ext3) keep such copies through journaling. However, "shred" is very useful for destroying the data on entire partitions or disks.
Some useful options are:
- -u, --remove
- unlink the file after removing it
- -NUMBER, -n NUMBER, --iterations=NUMBER
- the number of iterations of overwriting the file; default is 25 iterations
Examples: Remove and completely destroy secretfile from a filesystem that overwrites data in place and does not use journaling (for example, the UFS filesystem of *BSD). For the last step, after the data is destroyed, the "-u" option unlinks the file from the filesystem.
$ shred -u secretfile
Note that if secretfile has multiple hard links (with ln for example), it will continue to exist with those other names, but will contain only random data.
touch
touch lets you change the date on a file. Can also be used to create a blank file.
Examples
This will change the access date and time of filename to the current time. If filename doesn't exist it will create a blank file.
$ touch filename
df
df reports the amount of free disk space available on each partition.
$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/md0 5763508 207380 5263352 4% / /dev/md1 78819376 13722288 61093296 19% /home /dev/md4 23070564 4309572 17589056 20% /usr /dev/md2 5763508 1757404 3713328 33% /var /dev/md3 2877756 334740 2396832 13% /tmp
Reports disk usage in human readable format with block-sizes in Kilo,Mega,Gigabytes.
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/hda1 2.3G 2.1G 133M 95% / tmpfs 61M 8.0K 61M 1% /dev/shm /dev/hda2 2.0G 1.8G 113M 94% /usr
ln
ln creates links between files
Examples:
To make a soft (symbolic) link "hello" to the file "/home/alice/code/bin/world":
$ ln -s /home/alice/code/bin/world hello
chown
chown changes the owner and group of files. Normally, only root is allowed to do this, but if a user owns a file, then that user can change the group, but only to groups containing that user. On old systems, the ability of users to give files to other users caused abuses, so most systems prohibit non-root users from changing the owner of files.
Some useful options are:
- -R
- recursively change owner/group on an entire directory tree
- -h
- do not follow symbolic links i.e. changes owner of the link, not the target file
- -f
- indicate no errors if change failed
Examples:
Root changes the ownership of "/etc/test.cfg" and "/etc/shadow" to user root, group wheel:
# chown root:wheel /etc/test.cfg /etc/shadow
The same, but only changing the owner:
# chown root: /etc/{test.cfg,shadow}
The same, but only changing the group:
# chown :wheel /etc/{test.cfg,shadow}
Root gives every file in "/etc/ssh", including files in subdirectories, to user root, group wheel:
# chown -R root:wheel /etc/ssh
The same, but excluding files in directories (and the invisible files "/etc/ssh/.*" will also be missed):
# chown root:wheel /etc/ssh/*
User "tux" changes the directory "/usr/local/src/xc" from group "tux" to group "wheel". Tux is a member of both groups.
$ ls -ld /usr/local/src/xc drwxr-xr-x 11 tux tux 512 Sep 30 16:19 /usr/local/src/xc $ chown tux:wheel /usr/local/src/xc $ ls -ld /usr/local/src/xc drwxr-xr-x 11 tux wheel 512 Sep 30 16:19 /usr/local/src/xc
chmod
chmod changes permissions of files. One must be familiar with Unix file permissions to understand this command. There are three permissions: read ("r"), write ("w"), and execute ("x"). There are three sets of permissions: for the owning user of the file ("u"), for the group of the file ("g"), and for other users ("o").
For a file, "execute" means to run it as a program. For a directory, "execute" permission is required to use anything in that directory tree, so doing anything with "/usr/share/doc/README" requires execute permissions on all of "/", "/usr", "/usr/share", and "/usr/share/doc".
If you are interested in more advanced topics like the set-uid, set-gid, sticky bits and octal numbers, try reading the FreeBSD manual page at http://www.FreeBSD.org/cgi/man.cgi (type "chmod" in the form and submit).
A useful option is:
- -R
- recursively change or set permissions on an entire directory tree
Examples:
We wrote a shell script called "configure". We make it executable ("+x") and then execute it as a command. Usually, "+x" is the same as "u+x" or "ug+x", depending on the status of the file mode creation mask.
$ chmod +x configure $ ./configure
Allow the owning user to run "configure":
$ chmod u+x configure
Deny the group and other users from running "configure":
$ chmod go-x configure
For all users except the owner ("gw"), disable all access to "~/mail" and "~/private" ("-rwx"). This way, the contents are private and only their owner (or root) can access them.
$ chmod go-rwx ~/mail ~/private
Note that in the previous example, "-R" was not specified. By disabling the execute bit ("-x"), all files inside ~/{mail,private} are protected even if their group and other read bits are enabled. Thus, simply moving some file from inside ~/{mail,private} to some public place like /tmp can make the files available to other users again.
The "root" user wants to set up /usr/local/src so that all users in group "wsrc" (including "tux") can create files there. Root will continue to own the directory. This is done by changing the group of /usr/local/src to "wsrc" and then by granting to the group ("g") the read, write, and execute permissions ("+rwx").
# chown :wsrc /usr/local/src # chmod g+rwx /usr/local/src
All Unix-like systems should allow all users to create temporary files in "/tmp" and "/var/tmp". Thus root gives everyone ("a", short for "ugo") all permissions ("+rwx") on the files.
# chmod a+rwx /tmp /var/tmp
The problem with the above is that because all users have write access to /tmp and /var/tmp, every user can delete and rename files, even ones not created by them. For example, "tux" could create "/tmp/socket.3908" and another user could delete it or rename it to "/tmp/garbage", thus annoying Tux. To keep temporary files safe, we use the sticky bit called "t". This limits the deletion and renaming of files in /tmp to root, the owner of /tmp (also root), and the owner of the file (Tux for "/tmp/socket.3908"). It does the same for /var/tmp. So what we should do is:
# chmod a+rwxt /tmp /var/tmp
Finding Files
find
find searches a given path for a file or folder. The syntax is: find [path...] [expression...]
Examples: On some of the latest Unix-like OS's, -print options can be avoided as it is the default. The following command searches for the file 'grub.conf' starting at the root ('/') directory.
$ find / -name grub.conf /etc/grub.conf
If you are not the administrator of the computer, you get error messages for all the directories you are not allowed to read. In this case do it like this for a bash shell:
$ find / -name grub.conf 2>/dev/null /etc/grub.conf
Or like this for a csh/tcsh:
$ find / -name grub.conf >& /dev/null /etc/grub.conf
The following command will search for all directories named 'local'.
$ find / -name local -type d /usr/X11R6/lib/X11/fonts/local /usr/local /var/cache/man/local /var/local
Tips: Using 'exec' option executes certain commands for each file found by find:
$ find . -name '*bak' -exec rm -i {} \;
rm: remove regular empty file `./file1.bak'? y
rm: remove regular empty file `./file2.bak'? y
rm: remove regular empty file `./file3.bak'? y
Using 'ok' has same effect except it will prompt for every file:
$ find . -name '*~' -ok rm {} \;
< rm ... ./RMAIL~ > ? y
- When using "-exec" or "-ok", a semicolon must be used to indicate the end of the arguments (to "rm" in the example). Because semicolon is special to the shell, it must be quoted. The above examples quote the semicolon with a backslash.
whereis
whereis searches the normal executable and man page locations for a specified file.
Examples:
$ whereis ls ls: /bin/ls /usr/bin/ls /usr/man/man1/ls.1.gz /usr/share/man/man1/ls.1.gz
which
which searches the locations in your PATH variable for a specified file. If you know a program is in your path (i.e you can run it) this is faster than whereis.
$ which pine /usr/bin/pine
locate
locate finds all filenames that match the specified query.
Examples:
$ locate make.conf /etc/make.conf /etc/make.conf.orig /etc/make.conf.example /usr/qt/3/mkspecs/linux-g++/qmake.conf /usr/share/man/man5/make.conf.5.gz
locate however, is a GNU software and the command is not a standard in traditional UNIX systems like Solaris. The locate command comes standard with Linux based systems.
grep
Examples:
- Search for a string in all the files at /etc :
grep -R -B3 -A4 "a string" /etc/*
Network
Setting up the network configuration
Normally you can set up the network configuration using the command netcardconfig. Otherwise, you can set up the network configuration using low level commands. For example, to set up or change the IP address of a computer using a gateway with IP 192.168.178.1 you can use the following commands:
1) Bring down the interface:
$ ifconfig eth0 inet down
2) Set up the new IP address:
$ ifconfig eth0 inet up 192.168.178.30 \
netmask 255.255.255.0 broadcast 192.168.178.255
3) Define the route through the gateway
$ route add default gw 192.168.178.1
4) Edit the file /etc/resolv.conf and add the string below:
nameserver 192.168.178.1
ifconfig
To see the actual network configuration use:
$ ifconfig -a
Others
loadkeys
loadkeys changes the keyboard layout. Example:
loadkeys /usr/share/kbd/keymaps/i386/qwertz/de-latin.map.gz
TAR
To extract the contents of a tar file, use the x (extract) flag in a command, like this:
tar xvf panda.tar
To pack a directory into a tar file, use the x (create) flag in a command, like this:
$ tar czf <file.tgz> <directory>
Example:
$ tar czf bkp-`date +%Y%m%d`.tgz wikidb
RPM
RPM stands for Red Hat Package Manager. For more information about the use of RPM see german website of SuSe Der Paket-Manager des SuSE Linux
Use the following command to install a package into the linux system:
$ rpm -i PAKET.rpm
It checks all the dependencies and show an error message if something goes wrong.
To remove a package use the following command:
$ rpm -e PAKET
To check if a package is installed and what is its status use the following command:
$ rpm -q -i PAKET
shutdown
To restart the system use the following command:
$ shutdown -r now

