SUSE Conversations


Linux Command Line I



By: DamianMyerscough

August 8, 2008 1:09 pm

Reads:1390

Comments:1

Rating:0

In this article we will look at working with Linux from the command line. We will look at some of the common commands used to utilized the SUSE Linux Enterprise distribution. The tasks that will be covered in this document are; managing files and directories, finding files, getting help, redirecting I/O and command chaining. This article is aimed at new users to the SUSE Linux Enterprise distribution.

Managing files and directories

In this section of the article we will look at moving files and directories around the file system, copying files and directories and finally creating and deleting files and directories. The first command that we will look at is mkdir which creates directories. The mkdir command takes one argument which is the directory name you would like to create as shown in Figure 2.1.

linux-y2v4:~ # mkdir test
linux-y2v4:~ # ls -ld test
drwxr-xr-x 2 root root 48 2008-06-05 15:30 test

Figure 2.1: Creating a new directory.

As you can see from Figure 2.1 the directory was created successfully. The mkdir also accepts brace expansion which allows you to create multiple directories with one command as shown in Figure 2.2.

linux-y2v4:~ # mkdir tes{1,2,3,4}
linux-y2v4:~ # ls -ld tes*
drwxr-xr-x 2 root root 48 2008-06-05 15:33 tes1 
drwxr-xr-x 2 root root 48 2008-06-05 15:33 tes2 
drwxr-xr-x 2 root root 48 2008-06-05 15:33 tes3 
drwxr-xr-x 2 root root 48 2008-06-05 15:33 tes4 
drwxr-xr-x 2 root root 48 2008-06-05 15:30 test

Figure 2.2: Creating multiple directories with one command.

As you can see from Figure 2.2 four directories were created ‘tes1′, ‘tes2′, ‘tes3′ and ‘tes4′. The second command I executed contained an asterisk (*) this character is known as a wild card and means any character, for example the second command means display any files or directories in long listing format that start with the word ‘tes’.

The second command that we will look at is rmdir. This command removes directories from the file system. However, when using this command the directory has to be empty otherwise the directory will not be removed. The rmdir command takes one argument which is the name of the directory you would like to remove. The rmdir command also accepts brace expansion which is identical to mkdir. Figure 2.3 shows the command use to remove the ‘test’ directory and also how to remove multiple directories with brace expansion.

linux-y2v4:~ # rmdir test/
linux-y2v4:~ # rmdir tes{1,2,3,4}

Figure 2.3: Removing a directories.

The third command that we will look at is touch. This command allow you to create empty files. The touch command takes one argument which is the name of the file you would like to create as shown in Figure 2.4.

linux-y2v4:~ # touch test.txt
linux-y2v4:~ # ls -l test.txt
-rw-r--r-- 1 root root 0 2008-06-05 15:51 test.txt

Figure 2.4: Creating an empty file.

As you can see in Figure 2.4 the file was created successfully. The touch command can also be used to update the timestamp for a file or directory. The third command that we will look at is rm. This command is used to remove files. The rm command takes a wide variety of qualifiers but we will be just using it to simply remove a file so we will only supply one argument which will be the filename as shown in Figure 2.5.

linux-y2v4:~ # rm test.txt

Figure 2.5: Removing a single file.

The forth command that we will look at is cp. This command is used for copying files from one location to another. The cp command takes a wide variety of qualifiers, however, we will only be supplying two arguments with the cp command. The first argument is the filename of the file we would like to copy and the second argument is the destination of where we would like to copy the file to as shown in Figure 2.6.

linux-y2v4:~ # cp test.txt /home/
linux-y2v4:~ # ls -l /home
total 0 
-rw-r--r-- 1 root root 0 2008-06-05 16:23 test.txt

Figure 2.6: Copying a file from one location to another.

As you can see from Figure 2.6 the file was successfully copied to the /home directory. You will also notice that the original copy is still in its current location. The forth command that we will look at is mv. This command is used for renaming files and moving them from one location to another. The first example that we will look at is renaming the ‘test.txt‘ file to ‘old.txt‘, Figure 2.7 shows the command used to rename the file. The mv command takes a few qualifiers and requires at least to arguments, if you are moving a file from one location to another you have to specify the first argument as the filename and the second argument as the destination directory, if you are renaming a file your first argument should be the filename and the second argument should be its new name.

linux-y2v4:~ # mv test.txt old.txt
linux-y2v4:~ # ls -l old.txt
-rw-r--r-- 1 root root 0 2008-06-05 16:23 old.txt 
linux-y2v4:~ # ls -l test.txt
/bin/ls: test.txt: No such file or directory

Figure 2.7: Renaming ‘test.txt’ to ‘old.txt’.

As you can see in Figure 2.7 the file was successfully renamed and the ‘test.txt‘ filename no longer exists because it had been renamed. As I mention previously the mv command can be used to move files and directories from one location to another. Figure 2.8 shows how to move a text file from one location to another.

linux-y2v4:~ # mv old.txt /usr/src
linux-y2v4:~ # ls -l /usr/src/old.txt
-rw-r--r-- 1 root root 0 2008-06-05 16:23 /usr/src/old.txt

Figure 2.8: Moving the ‘old.txt’ file to a new location.

As you can see from Figure 2.8 the file was moved successfully and you should notice that the file is no longer in your current working directory.

Finding files and folders

In this section of the article we will look at finding files using two different methods. The first command that we will examine is find. This command has some very powerful features for finding files such as searching by: file access time, file size, filename, file contents, etc. The second command that we will examine is locate. This command is very simple to use. However, you will need to install the ‘findutils-locate‘ package which is stored on the SUSE Linux CD/DVD.

The first file that we will try and find is ‘passwd‘, the first command we will use is find. The find command requires two arguments and one qualifier, Figure 3.1 shows the command used to search for the ‘passwd‘ file. Table 3 explains each qualifier and argument shown in Figure 3.1.

linux-y2v4:~ # find / -iname passwd
/etc/default/passwd 
/etc/pam.d/passwd 
/etc/passwd 
/var/run/nscd/passwd 
/usr/bin/passwd

Figure 3.1: Searching for the ‘passwd’ file.

Qualifier Description
/ This argument is where you specify the path in which you would like to search.
-iname This qualifier tells the find command that we are performing a case insensitive search.
public_html This directory is used to store HTML content. When the Apache web server has been enabled and has the UserDir option enabled this directory is published on the Internet.
passwd This argument is where you specify the file which you would like to search for.

Table 1: find qualifiers and arguments explained.

The second example we will do with the find command is find a file which has the keyword ‘damian’. Figure 3.2 shows the command used to perform this search and Table 4 explains each qualifier and argument.

linux-y2v4:~ # find /etc -type f | xargs grep -l -i "damian"
/etc/opt/gnome/gconf/schemas/gnome-power-manager.schemas 
/etc/opt/gnome/gconf/gconf.xml.defaults/schemas/apps/gnome-power-manager/%gconf.xml 
/etc/group 
/etc/passwd 
/etc/shadow 
/etc/group.old 
/etc/services

Figure 3.2: Searching file content for the keyword ‘damian’.

Qualifier Description
/etc This argument is where you specify the path which you would like to search. In this example we search the /etc directory.
-type f This qualifier specifies that we only want to check regular files.
| xargs grep -l -i “damian” This section of the command retrieves the filename via a pipe (|) and passes the filename into the grep utility which search for the keyword ‘damian’ and prints out the filename which contains the keyword.

Table 2: find qualifiers and arguments explained.

As you can see in Figure 3.2 we piped the data from the find command into the xargs command, piping is explained later in this document. The last example that we will look at when using the find command is to find files that have a size greater than 500MB, Figure 3.3 shows the command used to search for files greater than 500MB.

linux-y2v4:~ # find /root -type f -size +500M -printf "The %p file is greater than 500MB\n"
The /root/junk.txt file is greater than 500MB 
linux-y2v4:~ # ls -lh junk.txt 
-rw-r--r-- 1 root root 1.1G 2008-06-06 08:47 junk.txt

Figure 3.3: Search for files greater than 500MB.

Qualifier Description
/root This argument is where you specify the path which you would like to search. In this example we are searching root’s home directory.
-type f This qualifier specifies that we only want to check regular files.
-size +500M This section of the command specifies the file size we would like to search for.
-printf “The %p file is greater than 500MB\n” This qualifier prints a custom message telling the user we found a file greater than 500MB.

Table 3: find qualifiers and arguments explained.

Once you have got the hang of the find command you should have no problems finding your files. I would recommend that you read the manual page for more information regarding the find command. Later in this article we look at using the manual pages.

The last command that we will look at for finding files is locate. This command is not as feature rich as the find utility but it is much easier to use. I am going to assume you already have the ‘findutils-locate‘ package installed. The task that you need to do before using the locate command is build a database of files and their location on the file system using the updatedb command as shown in Figure 3.4.

linux-y2v4:~ # updatedb

Figure 3.4: Updating the locate database.

Once you have updated the location database you can begin to use the locate command. The locate command takes only one argument which is the name of the file you are trying to find as shown in Figure 3.5.

linux-y2v4:~ # locate junk.txt
/root/junk.txt

Figure 3.5: Locating a file called: ‘junk.txt’.

The locate command works at a much faster speed than the find command. The reason for this is because the locate command just searches a database whereas the find command actually crawls through the directories looking for the file. There is one disadvantage to the locate utility and that is you need to update its database daily otherwise you will not get accurate readings i.e. deleted files will still be referenced in the locate database and will only be removed from the database when it has been updated.

Getting Help

In this section of the article we will look at getting help from the command line. The command used to retrieve help is man. This command takes one arguments which is the name of a command you want to find out how to use. The command man then returns a manual page. In the previous section we looked at the find command. This command took multiple qualifiers which I did not cover. However, you can see all the qualifiers along with an explanation of each qualifier in the find manual pages. Figure 4.1 shows the command use to open the manual page for the find utility.

linux-y2v4:~ # man find

Figure 4.1: Opening the manual page for the find command.

Once you have issued the man command with the ‘find’ keyword you will be presented with a new screen which will display all the information about the find command. You can use the up and down arrow keys to read the manual page and to exit you simple press the ‘q’ character.

Redirecting I/O

In this section of the article we will look at redirecting input and output to files and to programs. The two redirection characters that are used are the less than symbol (<) which redirects input and the greater than symbol (>) which redirects output.

The first example that we will look at is redirecting the output from the ls command to a text file called: ‘ls.txt‘ as shown in Figure 5.1.

linux-y2v4:~ # ls > ls.txt
linux-y2v4:~ # cat ls.txt
.bash_history 
bin 
.config 
...
...

Figure 5.1: Redirecting the output off the ‘ls’ command.

As you can see the output from the ls command was sent to the text file ‘ls.txt‘. The second example that we will look at is redirecting the input of a file. We will redirect the input of the ‘ls.txt‘ into the tac command which will display the ‘ls.txt‘ file in reverse. Figure 5.2 shows the command used to redirect the users input.

linux-y2v4:~ # tac < ls.txt
.xsession-errors 
.Xauthority 
.wapi 
.viminfo

Figure 5.2: Redirecting the users input.

As you can see in Figure 5.2 the input from the ‘ls.txt‘ file was redirected into the input of the tac command. In Linux it is also possible to redirect standard errors and standard output to files. Figure 5.3 shows an example of redirecting an errors to a file and the standard output to another.

linux-y2v4:~/Desktop # ls -l test 1> ls.txt 2> ls-error.log

Figure 5.3: Redirecting STDOUT and STDERR.

As you can see in Figure 5.3 the command above will produce an error because the ‘test’ file/directory does not exist, so what will happen is the error message will go into the ‘ls-error.log‘ file and no information will be directed into the ‘ls.txt‘ file. Table 6 lists the status codes that can be used with I/O redirection.

Code Description
0 This code is for standard input (STDIN).
1 This code is for standard output (STDOUT).
2 This code is for standard error (STDERR).

Table 4: I/O Status Codes.

The command shown in Figure 5.3 can also show the use of standard output for example, if you remove the keyword test you will notice that the output from the ls command will be directed into the ‘ls.txt‘ file and no information will be sent to the ‘ls-error.log‘ file.

Command chaining

In this section of the article we will look at command chaining. Command chaining is very useful as it allows you to run a group of commands which can be mixed with logical operators to decide which commands get executed. The first example we will look at is running two different commands on the same line as shown in Figure 6.1.

linux-y2v4:~/Desktop # date ; uptime
Fri Jun  6 10:29:07 BST 2008 
 10:29am  up  23:57,  3 users,  load average: 0.00, 0.01, 0.00

Figure 6.1: Chaining two commands together.

As you can see in Figure 6.1 the two commands were executed one after the other. You maybe wondering what happens if the first command fails. Will the second command still be executed? the answer is ‘Yes’. However, what if the second command relied on the first command to be successful? The solution to this little problem is to introduce logic. The logical operators that are supported are listed in Table 7.

Logical Operator Description
&& This is a logical AND when two commands are joined together the first command has to be successful before the second command is executed.
|| This is a logical OR when two commands are put together if the first command fails the second command runs, if the first command is successful then the second command is not executed.

Table 5: Logical operators.

The first example that we will look at is using logical operators with the two ampersands (&&) we will make the first command print a simple message and the second command display the current time as shown in Figure 6.2.

linux-y2v4:~ # echo "Hello World" && date 
Hello World 
Fri Jun  6 10:39:09 BST 2008

Figure 6.2: Performing a logical AND.

As you can see in Figure 6.2 both commands are executed. However, this time we will run an invalid command and display a message with the echo command. Since we are using the logical AND the echo command should not be executed thus not display this message.

linux-y2v4:~ # lds && echo "This Message Will NOT Be Displayed"
-bash: lds: command not found

Figure 6.3: Performing a logical AND.

As you can see in Figure 6.3 the second command is not executed, this is because the first command was unsuccessful. The second example that we will look is using is the logical OR (||) operator. We will execute the uptime command followed by the date command. However, you should notice that the date command will not be executed as shown in Figure 6.4.

linux-y2v4:~ # uptime || date 
 12:08pm  up 1 day  1:36,  4 users,  load average: 0.01, 0.02, 0.00

Figure 6.4: Performing a logical OR.

As you can see in Figure 6.4 the date command was not executed because the first command was successful, the only way for the second command to be executed is if the first command fails as shown in Figure 6.5.

linux-y2v4:~ # lsd || date
-bash: lsd: command not found 
Fri Jun  6 12:09:52 BST 2008

Figure 6.5: Performing a logical OR.

As you can see in Figure 6.5 the second command was executed successfully because the first one failed. In Linux it is possible to combined both the logical AND and the logical OR operators when working on the command line.

Final Thoughts

In this article we covered some of the basic topics in Linux. I hope that beginners to Linux find this article useful. This article was aimed at new users to show them how to use the SUSE Linux Enterprise distribution from the command line. You should now be able to manage files and directories, find files and retrieve help when needed.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Tags:
Categories: SUSE Linux Enterprise Desktop, SUSE Linux Enterprise Server, Technical Solutions

Disclaimer: As with everything else at SUSE Conversations, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.

1 Comment

  1. By:Anonymous

    Nice to see someone take the time to jot down some of the commands and how they can be used. I sometimes forget some of the less common syntax and always find it nice to find places on the net that have syntax examples with user friendly explinations.

    Cheers to you!

    –JWarren

Comment

RSS