Commands

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Commands as PDF for free.

More details

  • Words: 1,768
  • Pages: 8
1. ls It is very frequently used command. It redirects directory contents to standard output. If issued without any path it will list current directory (where we are at present). 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 spool tmp

mail

opt

run

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: • • • • • •

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. -rw-r–r–

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. ls

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: • • •

- displays contents sorted according to modification time (from latest to oldest), -S - displays contents according to size (from biggest to smallest), -r - reverses sort order. -t

There is also a possibility to list directories recursively, that is, to display contents of all subdirectories found in a given directory - it is served by -R parameter. 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

Another special character, namely -, denotes former directory. To jump to parent directory one needs to issue cd .. at the prompt. Every directory in Unix systems, even empty one, comprises of at least two parts . (dot) .. (double dot). One dot . means the same directory, double dots .. is a link to the parent directory in the directory tree. adam@laptop:~$cd .. adam@laptop:/home$pwd /home

Together with cd command it is worthwhile to introduce two new expressions - the relative path and the absolute path. • •

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. adam@laptop:~/Documents/polishlinux.org/examples$ mkdir directory adam@laptop:~/Documents/polishlinux.org/examples$ ls directory example.txt all_about_console.txt adam@laptop:~/Documents/polishlinux.org/examples$ rmdir directory/ adam@laptop:~/Documents/polishlinux.org/examples$ ls example.txt all_about_console.txt

4. cp, mv, rm • • •

- (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. cp

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),

and rm commands can work in recursive mode - thanks to -r (-R, –recursive parameters. cp

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. There are two kind of such links in Unix/Linux systems: •

symbolic links

- referring to a file - they can be considered as Windows short-

cuts, •

- referring to a disk area, in other words to a physical disk area where a file is located. hard links

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. All symlinks are created by ln command issued with -s parameter. adam@laptop:~/Documents/polishlinux.org/examples$ cat file Symlink example. 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$ ln file hardlink_file 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 adam@laptop:~/Documents/polishlinux.org/examples$ touch example.txt adam@laptop:~/Documents/polishlinux.org/examples$ ls -l total 0 -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 new.txt 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

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 /dev/sda5 varrun varlock procbususb udev devshm

Size 40G 502M 502M 502M 502M 502M

Used 34G 136K 0 148K 148K 0

Avail Capacity 4,0G 90% 502M 1% 502M 0% 502M 1% 502M 1% 502M 0%

Mounted on / /var/run /var/lock /proc/bus/usb /dev /dev/shm

And example for the du command: adam@laptop:~/Documents/polishlinux.org/examples$ du -h error.txt 4,0K error.txt

8. echo This simple command returns a text or a message attached to it as a parameter. adam@laptop:~$echo Hello World! Hello World! adam@laptop:~$

“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: adam@laptop:~$echo -n Hello World! Hello World!adam@laptop:~$

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 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: cat

adam@laptop:~$cat > file.txt Very interesting text.

We will expand on the redirection operator > later in this guide. To display file contents it suffices to type in cat file_name. 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.

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. cat

cat file1 file2 file3 file4 file5 > file.iso

11. wc, head, tail 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. adam@laptop:~/Documents/polishlinux.org$ head -n 1 example.txt The first row of the text. adam@laptop:~/Documents/polishlinux.org$ tail -n 2 example.txt The last but one row of the text. The last row of the text.

12. less less

enables us to scroll a text up and down on the screen.

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. adam@laptop:~$cat -n file.txt | less

Related Documents

Commands
May 2020 30
Commands
July 2020 19
Commands
November 2019 40
Commands
July 2020 16
Commands
April 2020 31
Commands
November 2019 45