Beruflich Dokumente
Kultur Dokumente
ls
adam@laptop:~/Documents/polishlinux.org$ pwd
/home/adam/Documents/polishlinux.org/
adam@laptop:~/Documents/polishlinux.org$ls
example.txt all_about_console.txt
adam@laptop:~/Documents/polishlinux.org$ls /var/
backups cache crash games lib local lock log mail opt run
spool
tmp
Second example in the listing above shows ls invoked with “/var/” directory path. The ls
command can reveal more detailed data if run with the “-l” parameter.
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
total 0
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 example.txt
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 all_about_console.txt
The first row of ls output shows total memory blocks taken by files from the directory.
The next rows are ordered as follows:
• -rw-r–r– file and directory permissions (more about permissions in the next part
of this guide),
• number of hard links to the file,
• file owner then a group the owner belongs to,
• file length,
• time of latest modification,
• file/directory name.
ls command can display hidden files (so called “dot files”) as well. Names of the hidden
files begin with a dot .. To show the files we need a -a parameter.
adam@laptop:~/Documents/polishlinux.org/examples$ ls -a
. .. .hidden_file example.txt all_about_console.txt
We are able to sort files using the ls command. The following parameters are available:
adam@laptop:~/Documents/polishlinux.org/examples$ ls -R
.:
directory1 directory2 example.txt all_about_console.txt
./directory1:
file1 file2
./directory2:
file3 file4
2. cd
Command change directory (cd) allows us to move through directory trees. It has one
parameter - target directory path.
adam@laptop:~$cd /usr/bin
adam@laptop:/usr/bin$pwd
/usr/bin
It is worth noting the tilde character ~ here. It is one of the special characters recognized
by all system shells. It denotes a path to the user’s home directory.
adam@laptop:/usr/bin$cd ~
adam@laptop:~$pwd
/home/adam
When we enter cd ~user’s_name the system will move us to that user’s home directory.
adam@laptop:~$cd ~zoidberg
adam@laptop:/home/zoidberg$pwd
/home/zoidberg
adam@laptop:~$cd ..
adam@laptop:/home$pwd
/home
• Absolute path is a path started from root of a directory tree “/”, e.g. /home/adam.
• Relative path is a path whose name begin with current directory, e.g. adam/
from /home/.
3. mkdir, rmdir
Both command deal with directories - the first one creates them, the second one deletes
them. Both make use of one parameter - a directory path. rmdir can only be performed on
empty directories.
4. cp, mv, rm
• cp - (copy) is self explanatory, it uses at least two parameters: a source file and a
target location to which the file will be copied,
• mv - (move) is used to change the location of directories and files or renaming
them. It works in similar way as the cp command,
• rm - (remove) removes files/directories.
The commands: cp, mv, and rm have the following common parameters:
• -f force - forces removing a file, even if user has no rights to write the file,
• -i interactive - user will be asked to conform the operation,
• -b -backup - creates backup copy of a file to be overwritten (for cp and mv),
adam@laptop:~/Documents/polishlinux.org/examples$ ls
directory1 directory2 directory3 example.txt all_about_console.txt
adam@laptop:~/Documents/polishlinux.org/examples$ cp -R directory2/
directory3/
adam@laptop:~/Documents/polishlinux.org/examples$ ls directory3/
directory2
adam@laptop:~/Documents/polishlinux.org/examples$ rm -r directory3/
adam@laptop:~/Documents/polishlinux.org/examples$ ls
directory1 directory2 example.txt all_about_console.txt
Beware! Command rm -r will remove ALL nested directories (not only empty ones), as
in the above example.
5. ln
The “ln” utility creates a new directory entry (linked file) which has the same properties
as the original file. Symlink (for short) can be thought as a sort of a short-cut known from
Windows systems.
If a file to which a symbolic link was created is removed (deleted), the symlink will be
listed in red (provided the shell supports colors). If we delete a file with a hard link
nothing will happen. The file won’t be erased until the number of hard links equals zero.
adam@laptop:~/Documents/polishlinux.org/examples$ ln -s file
symlink_file
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
lrwxrwxrwx 1 adam adam 4 2007-06-01 19:11 symlink_file -> file
-rw-r--r-- 1 adam adam 22 2007-06-01 19:10 file
adam@laptop:~/Documents/polishlinux.org/examples$ rm file
adam@laptop:~/Documents/polishlinux.org/examples$ ls
symlink_file
The above listing shows symbolic link in action. ls (with -l) tells us the number of hard
links for a “file” file remained the same. Contrary to the below example where number of
two hard links had diminished to one after a file deletion and the “file” file was still
present.
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
-rw-r--r-- 2 adam adam 22 2007-06-01 19:11 hardlink_file
-rw-r--r-- 2 adam adam 22 2007-06-01 19:11 file
adam@laptop:~/Documents/polishlinux.org/examples$ rm file
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
-rw-r--r-- 1 adam adam 22 2007-06-01 19:11 hardlink_file
6. touch
This simple command has two applications. If we add as a parameter an existing file, the
command will change the file’s modification time. If the file does not exist it will be
created.
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l example.txt
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 example.txt
When touch is invoked with -c or –no-create parameters we will prevent the file
creation. Other parameters -d and -t change the access and modification times to the
specified time. The “-t” needs to have a date specified in the form of
[[CC]YY]MMDDhhmm[.ss].
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
total 0
-rw-r--r-- 1 adam adam 0 2007-06-07 13:28 new.txt
-rw-r--r-- 1 adam adam 0 2007-06-07 13:27 example.txt
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 all_about_console.txt
adam@laptop:~/Documents/polishlinux.org/examples$ touch -t
200706101200 new.txt
adam@laptop:~/Documents/polishlinux.org/examples$ touch -d
"last monday" example.txt
adam@laptop:~/Documents/polishlinux.org/examples$ touch -d
"2 days ago 12:00" all_about_console.txt
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
total 0
-rw-r--r-- 1 adam adam 0 2007-06-10 12:00 new.txt
-rw-r--r-- 1 adam adam 0 2007-06-04 00:00 example.txt
-rw-r--r-- 1 adam adam 0 2007-06-05 12:00 all_about_console.txt
7. df, du
Next shell commands - df and du - display free disk space - df for the whole filesystem
and du for a given file. If run with -h attribute they will show the size in human readable
format rather than blocks. For example:
adam@laptop:~/Documents/polishlinux.org/examples$ df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/sda5 40G 34G 4,0G 90% /
varrun 502M 136K 502M 1% /var/run
varlock 502M 0 502M 0% /var/lock
procbususb 502M 148K 502M 1% /proc/bus/usb
udev 502M 148K 502M 1% /dev
devshm 502M 0 502M 0% /dev/shm
adam@laptop:~/Documents/polishlinux.org/examples$ du -h error.txt
4,0K error.txt
8. echo
“Echo” adds at the end of a line a newline character. To get rid of it one should run the
command with -n. For example:
When the text is enclosed in quotation marks it will be interpreted directly (but we will
tell about it later).
9. pwd
pwd (print working directory) shows full path of the current directory.
adam@laptop:~$pwd
/home/adam
adam@laptop:~$
10. cat
cat command can be used for a file creation. To put it more correctly it can be used to
redirect standard input to a file and to display it on standard output. Files are created in
the following way:
adam@laptop:~$cat file.txt
Very interesting text.
One of its parameters prints line numbers when “cat” is used to shows a file contents.
adam@laptop:~$cat -n file.txt
1 Very interesting text.
cat enables us to concatenate several files together, indeed cat is shorthand for
concatenate. In the following example five files are to be merged in one “file.iso” file.
These three commands are used to process text strings. wc command displays a number
of lines, a number of words, and a number of bytes for a given file.
adam@laptop:~/Documents/polishlinux.org$ wc all_about_console.txt
94 908 6828 all_about_console.txt
The numbers in the above example refer to (from left to right) - lines, words, and bits -
found in all_about_console.txt file.
Two successive commands display accordingly: the head - a file beginning, and the tail
- a file tail (ending). Both commands show 10 rows of a file by default. The number can
be changed with the -n number parameter.
12. less
adam@laptop:~$less file.txt
Running the command as shown above will display file.txt contents on a screen. Entering
:f during browsing the file will show interesting details, for example the line number of
the line displayed at the upper edge of monitor’s screen or the file size. Joining the
command with the “cat -n” command will display the contents supplemented by line
numbers placed at the left side of a screen.