Basic Unix Linux commands - Examples

From xotHostWiki
Jump to: navigation, search

Contents

Real Life Example

This section tries to cover as much as possible of what is discussed in the Basic Unix / Linux commands article with a real example where we will change directories, create and modify files and much more.

Example

We just logged in our server

$ _

Display the current working directory:

  • We want to know in which directory we are (our user is luis)
$ pwd
/home/luis

Create a directory:

  • We create the directory where we will work:
$ mkdir things

Dsiplay the contents of a directory:

  • We check the contents of our home directory:
$ ls -al
drwxr-xr-x 4 luis users 4096 May 5 20:19 .
drwx------ 62 luis users 4096 May 5 20:18 ..
-rw-r--r-- 1 luis users 0 May 5 20:19 colors.txt
drwxr-xr-x 2 luis users 4096 May 5 20:18 Documents
-rw-r--r-- 1 luis users 0 May 5 20:18 .ssh
drwxr-xr-x 2 luis users 4096 May 5 20:19 things

Move to another directory:

  • We change into the things directory
$ cd th[TAB]ings

(remember we can complete what we are writing with the TAB button)

$ pwd
/home/luis/things

Creating an empty file:

  • Create 3 empty files: animals.txt, cars.txt and countires.txt
$ touch animals.txt cars.txt countires.txt
$ ls -al
drwxr-xr-x 2 luis users 4096 May 5 20:22 .
drwxr-xr-x 4 luis users 4096 May 5 20:19 ..
-rw-r--r-- 1 luis users 0 May 5 20:22 animals.txt
-rw-r--r-- 1 luis users 0 May 5 20:22 cars.txt
-rw-r--r-- 1 luis users 0 May 5 20:22 countires.txt

Copy a file from a higher directory:

  • On the previous directory we have a file named colors.txt. We want to copy it to our present directory:
$ cp ../colors.txt .
$ ls -al
drwxr-xr-x 2 luis users 4096 May 5 20:24 .
drwxr-xr-x 4 luis users 4096 May 5 20:24 ..
-rw-r--r-- 1 luis users 0 May 5 20:22 animals.txt
-rw-r--r-- 1 luis users 0 May 5 20:22 cars.txt
-rw-r--r-- 1 luis users 0 May 5 20:19 colors.txt
-rw-r--r-- 1 luis users 0 May 5 20:22 countires.txt

Put some content on a file:

$ echo Dog > animals.txt
$ echo Cat >> animals.txt
$ echo Horse >> animals.txt
$ echo Donkey >> animals.txt
Lets see the result:
$ cat animals.txt
Dog
Cat
Horse
Donkey

Put some more content on the other files

  • We put content on the rest of the files. This step is ommited since it is just a repetition of the above command.
$ ls -al
total 24
drwxr-xr-x 2 amerediz users 4096 May 5 20:33 .
drwxr-xr-x 4 amerediz users 4096 May 5 20:24 ..
-rw-r--r-- 1 amerediz users 21 May 5 20:27 animals.txt
-rw-r--r-- 1 amerediz users 30 May 5 20:30 cars.txt
-rw-r--r-- 1 amerediz users 36 May 5 20:30 colors.txt
-rw-r--r-- 1 amerediz users 84 May 5 20:32 countires.txt

(note that our files are not empty anymore)

Rename a file

  • Lets assume we want to show the directory to our British friend, so he will like to see "colours" instead of "colors"
$ mv colors.txt colours.txt

Check for a string in a file

  • We want to see if the country Kenya is included in the countries.txt file:
$ grep ken countires.txt
$ _

(maybe Kenya is written with capital letter) $ grep -i ken countires.txt Kenya is in Africa

Copy the contents of a file into another file:

  • Now we want to put the contents of all the files in a single file called everything.txt
$ cat cars.txt > everything.txt
$ cat everything.txt
ferrari
maserati
lamborghini
$ cat colours.txt > everything.txt
$ cat everything.txt
red
orange
yellow
green
blue
purple

(OOps!) We deleted the contents we had before). The correct way is:

$ cat animals.txt > everything.txt 
$ cat cars.txt >> everything.txt 
$ cat colours.txt >> everything.txt 
$ cat countires.txt >> everything.txt 
$ cat everything.txt 
Dog
Cat
Horse
Donkey
ferrari
maseratti
lamborghini
red
orange
yellow
green
blue
purple
Mexico is in the Americas
Portugal is in Europe
Japan is in Asia
Kenya is in Africa

Creating a symbolic link for a file

  • Now we link the everything.txt to the /home/luis/Documents directory:
$ ln -s everything.txt ~/Documents
$ ls -al ../Documents
total 8
drwxr-xr-x 2 luis users 4096 May 5 20:49 .
drwxr-xr-x 4 luis users 4096 May 5 20:24 ..
lrwxrwxrwx 1 luis users 14 May 5 20:49 everything.txt -> everything.txt

Changing file permissions:

  • We will make the file everything.txt readable and writable by the user and other users belonging to the group "users" but not for everyone else:
$ chmod 660 everything.txt
$ ls -al
total 28
drwxr-xr-x 2 luis users 4096 May 5 20:40 .
drwxr-xr-x 4 luis users 4096 May 5 20:24 ..
-rw-r--r-- 1 luis users 21 May 5 20:27 animals.txt
-rw-r--r-- 1 luis users 30 May 5 20:30 cars.txt
-rw-r--r-- 1 luis users 36 May 5 20:30 colours.txt
-rw-r--r-- 1 luis users 84 May 5 20:32 countires.txt
-rw-rw---- 1 luis users 171 May 5 20:45 everything.txt

(note the file everything.txt changed to permissions -rw-rw----

Changing file ownership:

  • To finish, we want to make the file everything.txt owned by luis and usable only for the users belonging to the group luis:
# chown luis:luis everything.txt

(the above executed as root)

$ ls -al
total 28
drwxr-xr-x 2 luis users 4096 May 5 20:40 .
drwxr-xr-x 4 luis users 4096 May 5 20:24 ..
-rw-r--r-- 1 luis users 21 May 5 20:27 animals.txt
-rw-r--r-- 1 luis users 30 May 5 20:30 cars.txt
-rw-r--r-- 1 luis users 36 May 5 20:30 colours.txt
-rw-r--r-- 1 luis users 84 May 5 20:32 countires.txt
-rw-rw---- 1 luis luis 171 May 5 20:45 everything.txt

Back to Main Page
Go to xotHost.com

Personal tools
Namespaces

Variants
Actions
Navigation
Tools