Thursday, May 5, 2011

Learning the Linux Terminal

This blog entry will cover some basic Linux commands to get you started in the terminal.  Commands are in no particular order.  Note: Some of these commands can vary depending on the distro.  They are all just binary files.  I made this a while ago, pretty much just for fun, and I wanted to see if I could be of some help. I'm not sure it was everything I wanted it to be at the time.  For a more comprehensive list of commands try this website.

Helpful Tip: Most commands will come with a help file. To access it you generally type in the command followed by --help, or -h. This will list some more advanced features of the commands which I wont be covering most of here.

history - View commands previously typed in.

pwd - Which stands for present working directory will tell you where you are located on the system.

cd - If you type cd followed by a directory you can switch to that directory.

ls - Which stands for list will list all the contents of the current directory. If you would like to view some extra info, such as the permissions, use "ls -l". If you would like to include hidden files type "ls -a". You can implement both at the same type if you like by typing "ls -la".

rm - Which stands for remove can delete files. Common options are "rm -r", the r standing for recursive, which deletes directories. "rm -f", the f standing for force will delete files without prompting various warnings.

cp - Which stands for copy will copy a file.

mv - Which stands for move will move a file.

chmod - Which stands for change mode, will change the permissions of the files. Adding a value of 1 will allow you to execute. Adding a value of 2 will let you write. Adding a value of 4 will allow you to read. You have to enter a combination of these 3 values added together 3 times. The order is "Owner Group Users". The command "chmod 777 [file]" will allow everyone to read write and execute. A directory must have the ability to execute in order to be opened.

adduser - Add a user.

groupadd - Add a group.

groupdel - Delete a group.

chgrp - Change the group of a specific file. Every file and directory has a said owner and group. To view these type "ls -l".

usermod - Which stands for user modification can do a variety of things. If you want to add a user to a group, without changing the other groups the user is in type "usermod -G [group] -a [user]".

df - Will list the filesystems and tell you how much disk space you have left in them.

du - Will list all the files on the disk and tell you how much space they take up.

ifconfig - Will list internet devices.  You are given IP addresses, mac addresses and the like so you can check on everything.

mount - Will tell you what's mounted.  You can also use this to mount drives.  For example "mount /dev/sda1 /mnt/drive" will mount the sda1 partition in the directory /mnt/drive.

fdisk - You could tell fdisk which device you would like to partition by typing in it's name ex("fdisk /dev/sda"), or you could have it list all of the devices by typing "fdisk -l".

Helpful tip:
You can use some commands together with a pipe. "|" is a pipe.

less - Is a command which will let you view the output of a command that might be too long to scroll through on the terminal. Or perhaps you're not able to scroll through your terminal, you can with this program. If you type "history | less" you will be able to scroll through the history of the commands you typed.

grep - This command will narrow down the output lines with specific output. It's kind of like the find feature. Lets say you want to look through your history to find every time you used the cp command. You could do this by typing "history | grep cp".

echo - Will output something to the screen. I will give an example of why this is useful down below.

Helpful tip:
If you want to write the output of a command to a file use > filename. For example if you wanted to add your history to a file named history you could type "history > history". If you wanted to first label this as your history you could type "echo "history:" > history && history >> history". The two greater than signs will append data to the file and wont delete what's already in there, while just one greater than sign will overwrite what's in there.

Added by request:

man - You can use this command to read the manual of a command.  Similar to typing --help.  This goes into more detail than help.

Helpful tip:
If you don't remember an exact command, but you remember the start of it, you can often press tab and have it auto-complete.  The same thing applies if you don't want to type in a long file name.  You start to type it in and then press tab and it will auto-complete.

1 comment:

  1. How can you not include the "man" command which can provide the user with integral knowledge about the command; eg "man man"? I would also include the tab auto-completion if the user forgets the exact command.

    ReplyDelete