Please feel free to email me with any questions at with "blog" as the subject.

Search my blog:

Loading...

Wednesday, 22 September 2010

Simple command line usage

I have been using the linux command line for some time and often when I post help I miss some steps as I take them as already known.

Usually most linux users will know them but noobs to linux may struggle and give up.

Here are a few simple commands that you may use quite often and a small explaination of what they do.

ls:
bash$ ls
This simply LiSts the files in the current directory. (If you have just opened the terminal this will be your home folder)
Here are some of the arguments you can use:
-a list hidden files
-d list the name of the current directory
-l long listing giving details about files and directories
-R list all subdirectories encountered
-t sort by time modified instead of name
There are more.

cd:
bash$ cd /Path/To/Directory
This allows you to change what directory you are in. You can cd into any directory you want.
For instance if
bash$ ls
Test

then you can:
bash$ cd Test
to go into directory Test.
You can also use the full location for instance:
bash$ cd /home/user/test
Additionally
bash$ cd ..
will take you to the directoy above the one you are browing.

mv:
This is used to change the name of a directory
Type mv followed by the current name of a directory and the new name of the directory.
bash$ mv TEST NEW-NAME-FOR-TEST

cp:
This is used to copy files.
bash$ cp FILE FILE2

mkdir:
This is the command to make a directory.
For instance:
bash$ mkdir TEST
would make a directory named TEST.

pwd:
This will show you the full path to the directory you are currently in.
This is very handy to use, especially when performing some of the other commands on this page
bash$ pwd

rm:
This is the ReMove command. You can only use this to delete individual files.
bash$ rm FILE
To delete a folder you must add args -r and -f for instance:
bash$ rm -rf TEST
but this permenantly removes files so is dangerous.
To be safer add the -i arg which will ask you if you're sure.

sudo:
In linux you are simply a user, not administrator. The administrator account is called the root account, but using this accound can be very dangerous.
For instance if you log in as root and type
bash$ rm -rf/
it will attempt to wipe the computer.
This is why sudo is used. Sudo lets you run a command as root from a user account.
Once sudo is set up (done automatically on most distros) you can simply:
bash$ sudo COMMAND-TO-RUN-AS-ROOT -ARGS
But be careful as:
bash$ sudo rm -rf /
is just as destructive as when logged in as root.

./
This is used to run a file that is not installed as a program. The file must have permissions to run.
bash$ ./FILE-TO-RUN

chmod
This is used to alter file permissions.
Usage is:
bash$ chmod [+/- x] [0-7 0-7 0-7]
A numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1. Omitted digits are assumed to be leading zeros. The first digit selects the set user ID (4) and set group ID (2) and restricted deletion or sticky (1) attributes. The second digit selects permissions for the user who owns the file: read (4), write (2), and execute (1); the third selects permissions for other users in the file's group, with the same values; and the fourth for other users not in the file's group, with the same values.


THat should get you used to command line. You can use:

bash$ COMMAND --help
or
bash$ help COMMAND
or
bash$ man -k COMMAND
to view the help for a command.

A good list of more commands is HERE