Linux: First 15 console commands newbies should learn

1. Super User – sudo and su

One of linux’ main security features is its permission system, which is very strict about who can do what. Many commands and operations are only available to the root user, and referenced as Super User. If you run into a permission denied when trying to do something from the command line, you need to let the system know that you have clearance to do so by becoming super user (you will need to enter the root password.)

There are two slightly different means of doing this. The first is sudo, which allows a user to run only certain predefined commands with super use permissions. This is useful if for example, you want a user to be able to install a program, but not have the ability to do anything else. The reason this is useful is because having unlimited root access can allow the user to hurt the computer if they do not know what they are doing. sudo also only works with one command at a time. Usage of sudo:

sudo command-to-execute

Here is an example (of editing the apache config file):

sudo vi /etc/httpd/conf/httpd.conf

You will be prompted for root password. The password you enter is invisible, so don’t panic when you don’t see anything while typing in the password. By default, sudo will be very limited to what commands are allowed.

The other method, which I use every time, is su which will in essence, create a session as root within the console, giving you unlimited permission over the system. Use care when doing this! The usage is very simple. All you have to do is enter su and hit enter.

su

When you are done with your business as root, simply type exit to end the session.

2. Change Directory – cd

Navigating your files with commands might seem daunting at first, but it is quite easy once you get the hang of it. This command as well as the next few will have you flying through your hard drive via console in no time.

To change directories, you use the cd command which stands for change directory. An example of usage:

cd /home
cd /etc
cd /var/log
cd ../  this will take you up one directory

3. List – ls

Of course a huge part of being able to navigate your hard drive, is being able to see what files/directories there are! Using the simple command ls you can view the contents of the current directory (or any path specified – see usage example). There are a lot of useful switches associated with ls, meaning you can tell ls how to behave when executing it by including a little extra information in the command. Examples:

ls Displays all files/folders EXCEPT hidden
ls -a Displays all files/folders, including hidden 
ls -l Displays files with more details
ls -s Displays file sizes 
ls -sh Displays file sizes in a easier to read format (i.e. 4.0K vs 4096) 
ls -lh Combines -l and -sh - List with easy-readable sizes

ls --help View a list of all available options

4. Copy – cp

The cp command is used to copy a file or folder.

cp source-file.ext path/to/copy/to/filename.ext
cp -f source destination This will force overwrite without asking.
cp -r source-folder destination This will copy a folder's contents and subfolders recursively.

cp --help View all available options

Remember you can use more than one switch at a time. For example, you could use -fr to force overwrites while doing a recursive copy.

5. Move – mv

The mv command works much the same way as copy does, except it will delete the source file(s) once the duplication has been completed.

mv source-file.ext path/to/move/to/filename.ext
mv -f source destination This will force overwrite without asking.
mv -r source-folder destination This will move a folder's contents and subfolders recursively.

mv --help View all available options

6. Delete – rm

The rm command is used for deleting files and folders. Always be cautious when using this command, especially when using a force or recursive option.

rm filename.ext
rm -f filename.ext Force delete without confirmation.
rm -r folder/ Remove folder and all sub folders USE CAUTION!

7. Make Directory – mkdir

Creating a new directory is easy with the mkdir command!

mkdir directory-name

8. New File – touch

This is probably the coolest command there is because of its name; touch will create a new empty file, or if the file already exists, will update the file’s accessed/modified time.

touch filename

9. Text Editor – vim

vim is a neat file editor that is very fast and light weight, however it can be quite confusing to learn the commands. There are plenty of sites out there with command listings and explanations. 

10. Display File – cat

This command is pretty simple but very helpful to view a file’s contents quickly without opening an editor.

cat filename

11. Searching – grep

The grep command is a little more complex but rightful in its placement of the top 15 linux commands to learn by heart! Grep will search a file, text, or other input and return any lines that contain data which you specify. Lets say you need to look in a log file for all errors that have to do with mysql. Instead of having to look through the whole file yourself and try to spot the lines you need, grep will do it for you!

grep 'mysql' log-file.log
grep -i 'search string' file Ignore case sensitivity
grep 'search string' file1 file2 file3 Search multiple files!
some-command | grep 'search string' search the output of a command

grep --help List all of the available options - theres a lot!

12. File Permissions – chmod

Sometimes you need to change a file’s permissions in order for other users and groups to read, write, or execute them. There are two commands that allow you to do this. The first is chown which actually changes the ownership of the file.

chown new-user filename

The second command is chmod which will let you modify the permissions on the file. Here are a couple simple examples:

chmod +x file chmod 775 file

For more information about how to use chmod to change more specific permissions based on users and groups, see this Wikipedia Article On chmod

13. Process Table – top

top is a handy program that will display the top resource-hungry processes as well as some information about your cpu usage, uptime, users, and memory usage.

14. Shutdown/Restart – shutdown

shutdown now Shuts down immediately
shutdown -t 10 Shuts down in 10 seconds
shutdown -r now Restarts computer

15. Services Interface – service

This command lets you stop, start, and restart services

service service-name [start|stop|restart]

service httpd start
service mysql stop