First steps in the shell

From openSUSE

Geeko The notation 'Shell' refers to a Terminal Window, such as Konsole in KDE. Konsole is an extremely powerful utility as it is possible to input a wide range of commands. See the #Basic Commands section to see some of the first steps which you may take when using Konsole.


Contents

Introduction

This short guide aims to give you your first taste of using the 'Shell', you will find many of the UNIX commands that are used in the shell can take input from another command; this, when grouped with the fact that there are so many commands that different small tasks, enables you to connect several commands together, and is one of the reasons the Shell is so powerful.

Basic Commands

Getting Around

pwd

pwd command prints the current/working directory.

ls

The ls command is used to list the contents of a directory. Very basic usage such as listing the contents of your current directory would look like:

ls

To see all files in the directory (including hidden files), add the "-a" argument:

ls -a

To receive a more detailed output, you can either append the -l output:

ls -l

Or use the alias:

ll

To combine both just add both '-l' and '-a' arguments:

ls -la

Or use the alias:

la

cd

cd command is used to change your current working directory.

cd dirname

will enter dirname directory.

cd

or

cd ~

will return to your home directory.

cd ~/directory

can be used to go to a directory in your home directory.

cd /directory

will go to a directory in the root directory.

cd directory

will go to a directory in the current directory.

cd ..

will go up one level (eg from 'home/foo/bar' to 'home/foo').

working with files

touch

This command is used to update last access time to the file. If such file is not exist it will be created.

touch hello.txt

in our case this command will create file called hello.txt

cp

The cp command allows a user to copy a file from one place to another. Say you have a file called help.txt and you wanted to make a copy to back it up to a file called help_backup.txt, you would perform that using the following command:

cp help.txt help_backup.txt

You can also do this with directories. Say you have a directory called Music and you wanted to create a backup directory of Music called Music_Backup, you would run the following command:

cp -d Music Music_Backup

The problem with this is that you will only be copying the directory and not the contents inside. In order to do that you would run:

cp -dr Music Music_Backup

mv

The mv command is used to move files from one place to another. For example, to move the file hello.txt from the current directory to the desktop you would use the following:

mv hello.txt ~/Desktop

You can move the file back to the current directory with:

mv ~/Desktop/hello.txt hello.txt

You can also use mv to rename files. For example, to rename hello.txt to bye.txt you would enter the following:

mv hello.txt bye.txt

By combining these two features, we could perform a move and rename together, as shown in the two examples below:

mv hello.txt ~/Desktop/bye.txt
mv ~/Desktop/bye.txt hello_again.txt

Here, we have moved the file hello.txt from the current directory to the desktop and named it bye.txt then we moved it back from the desktop to the current directory and named it hello_again.txt

Printing a Files Contents

cat

cat command is used to read and out put content of the file provided as argument.

cat hello.txt

will return content of the hello.txt file.

hello world!
hello again!

grep

grep searches the it's input for lines containing a match to the given pattern. grep can be aslo used for searching the files.

grep 'again' hello.txt

will return all string containing again pattern.

hello again!

less

less command is used to paginate the output. Very often when the output is long, it is piped to less.

ls -a | less

will pipe the output of ls -a to less; the files in the current directory will then appear page-wise. You can proceed using Page Down button, space bar. You can stop by pressing q.

more

more command is similar to less; however ENTER is used to proceed, the line number isn't given and it automatically exits when the end is reached (q can still be used to stop).

cat foo | more

will pipe the contents of foo to more.

head

head prints the first N lines of a file (default is 10), or output from another command, to the screen.

head foo

will print the first 10 lines of foo to the screen.

head -5 foo

will print the first 5 lines of foo to the screen.

ls -a | head

will pipe the output of ls -a to head, and therefore print the first 10 lines of files in the current directory.

tail

tail is similar to head, but prints the last N lines of a file, or output from another command, to the screen.

tail foo

will print the last 10 lines of foo to the screen.

tail -15 foo

will print the last 15 lines of foo to the screen.

working with directories

mkdir

mkdir command creates new directory.

mkdir dirname

will create directory named dirname

connecting commands

pipe

pipe '|' is used to redirect output of one command to another. For example:

cat hello.txt | grep 'world'

will return

hello world!

You can place any amount of the pipes in one command.

echo

This command is used to output exactly what you give it.

echo 'hello world!'

will output

 hello world!

You can also put output of this command to some file.

 echo 'hello world!' > hello.txt

Such command will remove all content of hello.txt file and place hello world! text into it. If you want to append some text to the file you can use >>

 echo 'hello again!' >> hello.txt

more information

man

man command is used to view the manual page for a given command, use up and down arrows to scroll through, SPACE BAR to page through and press 'q' to exit.

man cd

will present the manual page for the cd command.

man man

will give the manual page for the man command.

info

info command displays information about the specified command

info man

will display information about the man command