Linux Command Line II | SUSE Communities

Linux Command Line II

Share
Share

By Damian Myerscough/
Originally published at https://www.novell.com/communities/coolsolutions

In part two of “Linux Command Line” we are going to look at managing SUSE Linux Enterprise from the command line. This article is aimed at novice users of SUSE Linux Enterprise. In this article we will cover some more basic commands such as setting symbolic links (hard and soft), compressing files, setting BASH aliases and much more. The first article, “Linux Command Line I” is available at [1].

Symbolic Links

In this section of the article we will look at how to create hard and soft symbolic links. A symbolic link is a reference to another file similar to Microsoft Windows shortcuts. However, Linux has two types of symbolic links. The first type of symbolic link is ‘soft’ which points to filenames instead of the file’s inodes and the second type of symbolic link is ‘hard’ which actually points to the inodes and not the filename.

You cannot create hard links across multiple devices e.g. If you have created separate partitions for /home and /usr you cannot hard link any files from /usr to /home. However, you can create soft links across multiple devices.

The command that you use to create symbolic links is ‘ln‘. This command with no qualifiers and two arguments will create a hard link. However, if you want to create soft links you need to provide the -s qualifier. Figure 2.1 shows the syntax of create a symbolic link.

ln /path/to/target /path/to/destination

Figure 2.1: Symbolic Link Syntax.

As you can see in Figure 2.1 the first argument is the actual filename you would like to point to and the second argument is where you want the symbolic link (shortcut) to be. The first example creates a soft symbolic link for the ‘cal‘ utility which is stored within the /usr/bin directory. Figure 2.2 shows the command used to create the soft symbolic link.

linux-9sl8:~ # ln -s /usr/bin/cal /root/cal
linux-9sl8:~ # ls -l /root/cal
lrwxrwxrwx 1 root root 12 2008-08-21 18:25 /root/cal -> /usr/bin/cal

Figure 2.2: Creating a soft symbolic link to the ‘cal’ utility.

As you can see from Figure 2.2 the ‘cal‘ utility was successfully created and ‘ls -l’ shows that the /root/cal file points to the /usr/bin/cal file. The second example that we will look at is creating a hard symbolic link to the ‘cal‘ utility, Figure 2.3 shows the command used to create the hard symbolic link.

linux-9sl8:~ # ln /usr/bin/cal /usr/cal
linux-9sl8:~ # ls -l /usr/cal
-rwxr-xr-x 2 root root 15008 2008-04-22 00:56 /usr/cal

Figure 2.3: Creating a hard symbolic link of the ‘cal’ utility.

As you can see in Figure 2.3 the hard link was successfully created and it does not show a pointing arrow when issuing the ‘ls -l‘ command. This is because hard links point directly to inodes and not the filename. Figure 2.4 shows the ‘stat‘ command being executed which allows you to examine a file more thoroughly.

linux-9sl8:/usr # stat /usr/bin/cal
  File: `/usr/bin/cal'
  Size: 15008           Blocks: 32         IO Block: 4096   regular file
Device: 802h/2050d      Inode: 57553       Links: 2
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2008-04-22 00:56:02.000000000 +0100
Modify: 2008-04-22 00:56:02.000000000 +0100
Change: 2008-08-21 18:34:51.000000000 +0100

linux-9sl8:/usr # stat /usr/cal
  File: `/usr/cal'
  Size: 15008           Blocks: 32         IO Block: 4096   regular file
Device: 802h/2050d      Inode: 57553       Links: 2
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2008-04-22 00:56:02.000000000 +0100
Modify: 2008-04-22 00:56:02.000000000 +0100
Change: 2008-08-21 18:34:51.000000000 +0100

Figure 2.4: Checking the original and hard symbolic link thoroughly.

As you can see in Figure 2.4 the inodes are identical. However, if you run the ‘stat‘ command against the soft symbolic link we created earlier you will notice that the inodes differ.

Compressing Files

In this section of the article we will look at how to compress files and directories. The utilities that we will be working with are ‘tar‘ (The GNU version of the tar archiving utility) and ‘gzip‘ (Compress or expand files).

gzip is a compression utility and requires the assistance of tar when compressing directories. The first utility that we will examine is tar. tar is used for archiving multiple files and directories which later can be compressed using gzip. Figure 3.1 shows the syntax for tar.

tar archive_name.tar /directory/you/want/to/archive

Figure 3.1: ‘tar’ syntax.

The first example will involve using ‘tar‘ to archive the user Damian’s home directory. Figure 3.2 shows the command used to archive Damian’s home directory and Table 1 explains what each qualifier supplied with the ‘tar‘ do.

linux-9sl8:/home # tar -cf damian.tar /home/damian

Figure 3.2: Archiving Damian’s home directory.

Qualifier Description
-c Creates a new archive.
-f damian.tar Use archive file.

Table 1: ‘tar’ qualifiers explained.

Once you have issued the command shown in Figure 3.2 you will notice that you have a newly created archive within your current working directory. The archive can be viewed using the ‘tar‘ command as shown in Figure 3.3. Table 2 explains what each qualifier in Figure 3.3 does.

linux-9sl8:/home # tar -tf damian.tar
home/damian/
home/damian/bin/
home/damian/Documents/
home/damian/Documents/.directory
home/damian/Documents/New Document.ott
home/damian/Documents/New Spreadsheet.ots
home/damian/.dmrc
...
...

Figure 3.3: Viewing the contents of the damian.tar archive.

Qualifier Description
-t List the contents of an archive.
-f damian.tar Use archive file.

Table 2: ‘tar’ qualifiers explained.

As you can see in Figure 3.3 the contents of the ‘damian.tar’ archive are displayed. tar also allows you to exclude certain files and directories by using the ‘–exclude’ qualifier and you can also append files to a tar archive by using the ‘-A’ qualifier.

Once you know how to archive a directory and view the contents of a ‘tar‘ archive you will also want to know how to extract files from the archive. Figure 3.4 shows the command used to extract all the files and directories stored within a ‘tar’ archive. Table 3 explains what each qualifier does.

linux-9sl8:/home # tar xvf damian.tar
home/damian/
home/damian/bin/
home/damian/Documents/
home/damian/Documents/.directory
...
...

Figure 3.4: Extract all the files and directories from the “damian.tar” archive.

Qualifier Description
x Extract files from an archive.
v Verbosely list files processed.
f Use archive file.

Table 3: ‘tar’ qualifiers explained.

Now that you have an idea on how to use ‘tar‘ we will look at how to use gzip to compress files. The first file that we will compress is the ‘damian.tar’ which we created earlier. Figure 3.5 shows the command used to compress the ‘damian.tar’ archive. Table 4 list the qualifiers used in Figure 3.5 and some other useful qualifiers.

linux-9sl8:/home # gzip -9 damian.tar

Figure 3.5: Compressing the damian.tar archive.

Qualifier Description
-9 Compress better.
-1 Compress faster.
-d Decompress the compressed file.
-v Verbose mode.
-r Operate recursively on directories.
-l List compressed file contents.

Table 4: ‘gzip’ supported qualifiers.

Once you have issued the command shown in Figure 3.5 you will notice the file now has the extension .gz which is a sign that the compression was successful. The next step is to decompress the compressed file using ‘gzip‘. The process of decompressing the file is simple since you only need to supply the -d qualifier followed by the filename as shown in Figure 3.6.

linux-9sl8:/home # gzip -d damian.tar.gz

Figure 3.6: Decompressing files using gzip.

Once you have decompressed the ‘damian.tar.gz’ file you will notice that the prefix .gz disappears and you will notice the file has been decompressed.

File Determination

In this section of the article we will look at how to determine a file type e.g. Binary files, text files, compressed archives and many others. In Linux it is not necessary to give extensions to files, however most people do give extensions just for readability. What would you do if you had a directory full of files and none of the files had extensions? Well it’s simple! There is a utility called ‘file’ which determines the file type for any file. Figure 4.1 shows the command used to determine the file type of the ‘/usr/bin/cal’ utility.

linux-9sl8:/home # file /usr/bin/cal
/usr/bin/cal: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), for GNU/Linux 2.6.4, stripped

Figure 4.1: Determining the file type of ‘/usr/bin/cal’.

Creating swap Files

In this section of the article we will look at how to create swap files. A swap file is a special file that is located on the hard disk and is used as the virtual memory. The swap file will be used when all the RAM on your machine is occupied.

The first task is to create a file filled with zeros. In this article we will make our swap file 1GB and have it stored in the root of the file system (/). Figure 5.1 shows the command used to create the swap file.

linux-9sl8:~ # dd if=/dev/zero of=/swap_file bs=1024M count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 760.2 seconds, 1.4 MB/s

Figure 5.1: Creating a 1GB empty file.

Once you have created the 1GB file you can use the ‘mkswap‘ command followed by the path to the empty file as shown in Figure 5.2.

linux-9sl8:~ # mkswap /swap_file
Setting up swapspace version 1, size = 1073737 kB

Figure 5.2: Creating the swap file.

Once you have successfully created the swap file all you will need to do is turn the swap file on and this can be done by simply issuing the ‘swapon‘ command as shown in Figure 5.3.

linux-9sl8:~ # swapon /swap_file 

Figure 5.3: Enabling the swap file.

Once you have turned on the swap file you can issue the ‘swapon‘ command followed by the -s qualifier which displays the status for all swap files and partitions as shown in Figure 5.4.

linux-9sl8:~ # swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda1                               partition       1052216 67856   -1
/swap_file                              file            1048568 0       -2

Figure 5.4: Viewing all swap files and partitions.

As you can see from Figure 5.4 the priority of the swap file we created is set to -2 which means it’s set to a low priority. If you wanted to set this to a high priority you can issue the ‘swapon‘ command followed by the -p qualifier and then a priority. Figure 5.5 shows the command used to change to priority of our swap file.

linux-9sl8:~ # swapoff /swap_file
linux-9sl8:~ # swapon /swap_file -p 100
linux-9sl8:~ # swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda1                               partition       1052216 67856   -1
/swap_file                              file            1048568 0       100

Figure 5.5: Setting a swap priority.

The priority range is 0 to 32767 with 0 being least priority and 32767 being high priority.
When setting the priority make sure the swap file is turned off.

As you can see in Figure 5.5 the command used to turn swap partitions/files off is ‘swapoff‘ followed by the swap device.

Aliases

In this section of the article we will look at what aliases are and how to configure aliases for the BASH shell. Aliases in BASH allow you to create an alias which will map to a program or a specific command for example, in Microsoft Windows if you use the command prompt you can delete files by issuing the ‘del‘ command and in Linux you will need to use the ‘rm‘ command. However, it is possible to alias the ‘del‘ command to the ‘rm‘ command so that when a user types ‘del‘ in the BASH shell it will execute the ‘rm‘ command.

In this article we will set an alias for the ‘del‘ command which will execute the ‘rm‘ command. The first task you need to do is check to see if the ‘del‘ command is currently present on your machine you can do this by simply issuing the ‘del‘ command as shown in Figure 6.1.

linux-9sl8:~ # del
-bash: del: command not found

Figure 6.1: Determining if a ‘del’ alias has already been set.

As you can see in Figure 6.1 the ‘del‘ command could not be found so this tells us that there is no ‘del‘ command on our system or in our path. The command used to set aliases in BASH is ‘alias‘ followed by the alias you would like to set. Figure 6.2 shows the alias syntax.

alias ALIAS_NAME="Command to be executed"

Figure 6.2: Alias syntax.

The alias that we are going to set is for the ‘del‘ command which will execute the ‘rm‘ command. Figure 6.3 shows the command used to alias ‘del‘.

linux-9sl8:~ # alias del="rm"

Figure 6.3: Setting the ‘del’ alias.

Once you have set the ‘del‘ alias you can issue the ‘del‘ command again and this time you should see the usage of the ‘rm‘ command as shown in Figure 6.4.

linux-9sl8:~ # del
rm: missing operand
Try `rm --help' for more information.

Figure 6.4: Testing the ‘del’ alias.

As you can see from Figure 6.4 the ‘rm‘ command was executed when the user typed ‘del‘ this is a clear indication that the alias is working correctly. Once you are happy with setting aliases you will want to know how to unset aliases this can be done by using the ‘unalias’ command as shown in Figure 6.5.

linux-9sl8:~ # unalias del

Figure 6.5: Removing the ‘del’ alias.

Once you have unset the ‘del‘ alias you can issue the ‘del‘ command and you will notice that the alias no longer exists and will report errors.

History

In this section of the article we will look at the ‘history‘ command. The ‘history‘ command is a very useful command as it allows you to track your previously typed commands. If you type ‘history‘ on its own you can view the majority of the commands you have typed as shown in Figure 7.1.

linux-9sl8:~ # history
    1  exit
    2  del
    3  alias del="rm"
    4  env | grep -i del
    5  env
    6  help
    7  alias del="rm"
    8  ls -l /swap_file
    9  ls -lh /swap_file
   10  ps aux | grep -i swap
...
...

Figure 7.1: Viewing previously typed executed commands.

As you can see in Figure 7.1 the ‘history‘ command returned a list of commands along with a line number. This line number can be useful if you don’t want to type the command out again you can issue an explanation mark (!) followed by the line number and the command will be executed as shown in Figure 7.2.

linux-9sl8:~ # !32
ls -lh /swap_file
-rw-r--r-- 1 root root 849M 2008-08-25 20:03 /swap_file

Figure 7.2: Executing a command from the command history.

As you can see from Figure 7.2 the command with the line number of 32 was successfully executed. The ‘history‘ command can also be a little bit of a pain as some users may accidentally type there root password when trying to use the ‘su‘ command to become the root user and the password gets stored within the history. The ‘history‘ command provides a qualifier (-c) which will clear the all the history thus allowing you to erase an previous commands you typed or any accidental passwords you may have typed.

Final Thoughts

In this article we continued on from the “Linux Command Line I” article to include some more commands and tips for novice users. After reading this article you should be more confident in creating swap files, symbolic links and also managing your command history.

Reference

[1] https://www.suse.com/communities/blog/linux-command-line-i/

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

No comments yet

Avatar photo
21,243 views