MorkaLork Development

Interesting stuff I've picked up over the years...

Basic linux commands

2010-01-28 11:38:08 | 432 views | basic linux commands

Basic linux commands



This article will list some of the most basic commands in linux. With the use of these
commands you should be able to move somewhat freely within a linux environment.


Index







ls - list directory contents


Using the ls command you will get a list of the directory contents, here is an example:


maffelu@maffelu-laptop:~$ ls
Desktop Documents games Music Pictures Public Templates Videos


When we enter the command ls we get a list of the /home/maffelu/ directory contents. The ls commands also have several flags, the following might come in handy (to see the rest simply enter the command 'man ls' in your terminal):

-a
Lists all contents within a directory, including entries starting with '.':


maffelu@maffelu-laptop:~$ ls -a
. games .ICEauthority .recently-used.xbel
.. .gconf .icons .sudo_as_admin_successful
.bash_history .gconfd .local Templates
.bash_logout .gegl-0.0 .mozilla .themes
.bashrc .gftp Music .thumbnails
.cache .gimp-2.6 .nautilus .update-manager-core
.config .gksu.lock .ncftp .update-notifier
.dbus .gnome2 .openoffice.org Videos
Desktop .gnome2_private Pictures .Xauthority
.dmrc .gnupg .profile .xchat2
Documents .gstreamer-0.10 Public .xsession-errors
.esd_auth .gtk-bookmarks .pulse
.fontconfig .gvfs .pulse-cookie


-l
This lists the contents in a directory including information such as owner, size, created date etc. This could be used in combination with the -h flag. The -h flag rewrites numbers into human readable format (such as 4K instead of 4000):


maffelu@maffelu-laptop:~$ ls -lh
total 40K
drwxr-xr-x 3 maffelu maffelu 4.0K 2010-01-27 14:14 cpp
drwxr-xr-x 2 maffelu maffelu 4.0K 2010-01-27 06:20 Desktop
drwxr-xr-x 3 maffelu maffelu 4.0K 2010-01-26 12:10 Documents
drwxr-xr-x 3 maffelu maffelu 4.0K 2010-01-26 08:55 games
drwxr-xr-x 2 maffelu maffelu 4.0K 2010-01-03 15:19 Music
drwxr-xr-x 3 maffelu maffelu 4.0K 2010-01-06 10:43 Pictures
drwxr-xr-x 2 maffelu maffelu 4.0K 2010-01-03 15:19 Public
drwxr-xr-x 2 maffelu maffelu 4.0K 2010-01-03 15:19 Templates
-rw-r--r-- 1 maffelu maffelu 10 2010-01-27 12:16 testfile.txt
drwxr-xr-x 2 maffelu maffelu 4.0K 2010-01-03 15:19 Videos



--sort
This allows you to sort the contents. The following words are valid to use:



maffelu@maffelu-laptop:~$ ls --sort=size
cpp Desktop Documents games Music Pictures Public Templates Videos testfile.txt


Back to index!



pwd - get current working directory


This command can come in handy if you ever get lost or something. It will print out the current working directory:


maffelu@maffelu-laptop:~/cpp/executables$ pwd
/home/maffelu/cpp/executables


Back to index!



cd - change directory


The cd command is quite common and is rather crucial to know if you ever want to change directory. It does however enable you to do more than just change directory:

cd <dirname> Enter dir

maffelu@maffelu-laptop:~$ cd Music/
maffelu@maffelu-laptop:~/Music$


cd ../ or cd .. Move up one dir

maffelu@maffelu-laptop:~/Music$ cd ..
maffelu@maffelu-laptop:~$


cd ../Pictures Move up one dir and into another

maffelu@maffelu-laptop:~/Music$ cd ../Pictures/
maffelu@maffelu-laptop:~/Pictures$


cd Move to home dir

maffelu@maffelu-laptop:~/Music/ABBA$ cd
maffelu@maffelu-laptop:~$


Back to index!




mkdir - create a directory


The mkdir allows you to create a directory.


maffelu@maffelu-laptop:~$ ls
cpp Documents Music Public testfile.txt
Desktop games Pictures Templates Videos
maffelu@maffelu-laptop:~$ mkdir exampleDirectory
maffelu@maffelu-laptop:~$ ls
cpp Documents games Pictures Templates Videos
Desktop exampleDirectory Music Public testfile.txt


rmdir - remove an empty directory


The rmdir allows you to remove an empty directory.


maffelu@maffelu-laptop:~$ ls
cpp Documents games Pictures Templates Videos
Desktop exampleDirectory Music Public testfile.txt
maffelu@maffelu-laptop:~$ rmdir exampleDirectory/
maffelu@maffelu-laptop:~$ ls
cpp Documents Music Public testfile.txt
Desktop games Pictures Templates Videos


Back to index!



rm - remove files or directories


The rm command lets you remove, or unlink, files or directories. The rm command has several important options.

rm remove file

maffelu@maffelu-laptop:~/testDir$ ls
testFile2.txt testFile3.txt testFile4.txt testFile.txt
maffelu@maffelu-laptop:~/testDir$ rm testFile.txt
maffelu@maffelu-laptop:~/testDir$ ls
testFile2.txt testFile3.txt testFile4.txt


rm -v remove while outputting what's happening

maffelu@maffelu-laptop:~/testDir$ ls
testFile2.txt testFile3.txt testFile4.txt testFile.txt
maffelu@maffelu-laptop:~/testDir$ rm -v *
removed `testFile2.txt'
removed `testFile3.txt'
removed `testFile4.txt'
removed `testFile.txt'
maffelu@maffelu-laptop:~/testDir$



rm -r recursively removes a directory with all contents (including sub directories)

maffelu@maffelu-laptop:~/testDir$ ls subDir/
testFile4.txt
maffelu@maffelu-laptop:~/testDir$ rmdir subDir/
rmdir: failed to remove `subDir/': Directory not empty
maffelu@maffelu-laptop:~/testDir$ rm -r subDir/
maffelu@maffelu-laptop:~/testDir$


(Notice that the rmdir command throws an error since the directory is not empty)

Back to index!



cat - concatenate files and output them


Using cat on a file will output its contens in the terminal.

testFile1.txt
testfile

testFile2.txt:
testfile
this is just a test
so many tests thou...


maffelu@maffelu-laptop:~/testDir$ cat testFile2.txt testFile1.txt
testfile
this is just a test
so many tests thou...
testfile
maffelu@maffelu-laptop:~/testDir$


Back to index!



find - search for a file


Using the find command you can search a directory for a file/files.


maffelu@maffelu-laptop:~$ find /home/maffelu/ -name "test*.txt"
/home/maffelu/testDir/testFile2.txt
/home/maffelu/testDir/testFile3.txt
/home/maffelu/testDir/testFile1.txt
/home/maffelu/testfile.txt


The above command searched the /home/maffelu directory for all textfiles with names starting with 'test'.


maffelu@maffelu-laptop:~$ find . -size 5000k
./games/phobia3.linux.tar.bz2
./games/phobia3/pics/credits.rll
./games/phobia3/pics/end.rll
./games/phobia3/pics/start.rll
./games/xcom.tar.bz2
maffelu@maffelu-laptop:~$


The above command searched the current directory (home, in this case), for all files with a size larger than 5000kb.

Back to index!



top - display linux tasks


The top command will display the tasks currently being managed by the Linux kernel. It looks sort of like the windows task manager. You can interact with the interface to some extent. This is what it could look like:

Running 'top' in your terminal might look like this


less - display text in readable format


Less allows you to output text in a terminal with a scroll function starting from the top. This can be useful if you need to output either a large file or a big directory list.


maffelu@maffelu-laptop:~$ less testFile.txt


Back to index!



chmod - change permissions


The chmod command lets you change the permissions of a file for the user (you), the group and the others (whoever they might be).

The permissions are represented as a 3-bit integer or as letters and can be viewed by running the command 'ls -l'. A possible output could be this:

This is what the ls -l command could output

This is what happens if I run the ls -l command in my home/maffelu/testDir/ directory. Here is an explanation of what we are displaying:

The ls -l output explained!

The interesting stuff is the permissions column. There are actually for of them, if we look at it this way:

- rw- r-- r--

The first column has only one minus sign, this represents the file type, there are six of them:

SignType
-Regular file
dDirectory
lLink
cSpecial file
sSocket
pNamed pipe


The second column is the User (you) permission. The third column is the group permission and the fourth column is the permission for others.
These are the columns we want to alter, but before altering, there is a crucial difference that we must learn between changing permissions on a regular file and changing the permissions on a directory, as you get different results:

Regular fileDirectory
Read (4) Lets a user view the file Lets a user view the directory contents (ls)
Write (2) Lets a user edit the file Lets a user create or remove files in the directory
Execute (1) Lets a user execute the file Lets a user enter the directory (cd)


This means that while removing the x permission from a file keeps users from executing the file, removing x permission from a directory keeps people from entering it.

There are two ways of changing the permissions; using numbers or letters.

We'll start of with the numbers as it may be considered way more correct/hardcore/1337. The number represent a 3-bit integer meaning that 2 = 010 and 5 = 101.
Here's a list of all possibilities:

BinaryPermissionInt
000---0
001--x1
010-w-2
011-wx3
100r--4
101r-x5
110rw-6
111rwx7


Now, knowing this we can determine that setting a file permission to 7 would allow the user to read, write and execute the file whereas permission 5 would let the user read and execute, but not write.

When we set the permissions on a file with numbers we alway expect three integers, one for each group. If less than three digits are entered the leading digits will be considered a zero, meaning that 55 is the same as 055 (--- r-x r-x).

Examples:

Permission: --- --- ---

chmod 755 myFile.txt
Permission: rwx r-x r-x

chmod 701 myFile.txt
Permission: rwx --- --x

chmod 052 myFile.txt
Permission: --- r-x -w-


If we want to use letters instead of of numbers it works a bit differently. With letters we can add ( ), remove (-) or set (=) permissions. The letters are the same as we can see in the file permissions, r (read), w (write) and x (execute). There are however three more letters representing who´s permission we are modding: u (user/owner), g (group) and o (other).

Examples:
Permission: --- --- ---

chmod u x myFile.txt
--x --- ---

chmod g rw myFile.txt
--x rw- ---

chmod o r myFile.txt
--x rw- r--

chmod u r, o-r myFile.txt
r-x rw- ---

chmod g=x myFile.txt
r-x --x ---

That should get you started with chmod!

Back to index!



grep - Global | Regular Expression | Print


The grep command lets you search text using regular expressions and outputs the resulting line. Here is an example:


grep 'er' cherries.txt


The above command will look through the cherries.txt text file for all lines containing the letters 'er', so it would find cherry, cherries, Bernard, Perdon etc.

To be able to use grep you should have a basic understanding of regular expressions (regex) since that is what you will be using.

Here follows an example. The example will look through a text file, cherries.txt, and look for certain things. First we use cat to output the entire list, so you can see what it contains, and thus what we get and don't get with our grep:


maffelu@maffelu-laptop:~/testDir/greps$ cat cherries.txt
cherry
cherry pie
cherries
sherry
scary


Now we will use grep. We want to find all words containing the letter 'er' together, like cherry.


maffelu@maffelu-laptop:~/testDir/greps$ grep 'er' cherries.txt
cherry
cherry pie
cherries
sherry


We can see that the word 'scary' was not found.

Next we look for all the word that start with ch. To do this we use the circumflex, ^, sign to say 'we only want words starting with whatever follows the circumflex!'.


maffelu@maffelu-laptop:~/testDir/greps$ grep '^ch' cherries.txt
cherry
cherry pie
cherries


And as we can see, we only get lines containing words starting with 'ch'.

The grep command can also be used with another command using the pipe. Let's use grep together with ls:


maffelu@maffelu-laptop:~/testDir$ ls
greps someWebPage.html subDir testFile1.txt testFile2.txt testFile3.txt testFile.txt
maffelu@maffelu-laptop:~/testDir$ ls | grep '^test'
testFile1.txt
testFile2.txt
testFile3.txt
testFile.txt
maffelu@maffelu-laptop:~/testDir$


Here we only want to find files starting with 'test', and that's all we get.

However, these are not very good examples as we don't really need grep here. However, what if you want to find all grep related files in /bin? Aha, we've found a great use for grep since /bin contains a lot of files:


maffelu@maffelu-laptop:~/testDir$ ls /bin | grep 'grep'
bzegrep
bzfgrep
bzgrep
egrep
fgrep
grep
zegrep
zfgrep
zgrep


Right, there were quite some files related to grep (at least semantically).


Allright, that about does it for now, hope you've enjoyed the article!


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.