Tuesday, October 19, 2010

Linux CLI (Command Line Interface) Part 1

Hello everyone. For now I am just going to talk about the basics. This will be some basic commands you will find yourself using regularly. The CLI (Command Line Interface) is VERY powerful and should be leveraged. If you don't use it, well then it's time to make a change. =)

Ok the commands I will be covering are:
ls, cd, cp, mv, rm, rmdir, unzip, zip, clear, and the use of the &.

NOTE: Keep in mine "everything" in the command like "IS" case sensitive. So if a file or folder is capital, you must do the same when using commands.

ls: List directory

"ls" is like dir in windows it shows you the contents of a directory. Can you use dir ? Yes you can but it won't give you as much detail. "ls" has extra parameters that give you more detailed results (almost all programs do) but we wont get into that right now.

cd: Change Directory "cd [directory path]"

"cd" allows you to change from one directoy to another. Keep in mind that in Windows you change directories with a \ "backslash". Linux is not the same, we use a / "forwardslash" here, so be sure to keep that in mind. If you are changing to a directory that is already in your current location you don't need a / in front of the directory name. You only need to include it if you are coming from the root directory. (aka the beginning of the file system, not the root directory as in /root) Kind of like starting at C: in windows. You start there and then move to Documents and Settings or Windows directories.

mv: Move "mv [file or folder] [destination location]"

"mv" allows you to move files and directories to other locations. In order to use it you have to include the source file/folder and then the destination location. So say you wanted to move your family.jpg picture file from your "Home" directory to your "Picture" directory. To use the mv command it would look like this, "mv family.jpg Pictures/". So "family.jpg" being the file name and "Pictures/" being the destination. Notice there is no / before the "Picture" Directory and no path before the file name. That is because you are already in the location of both the file and directory.

rm: Remove "rm [filepath]"

"rm" grants you the ability to remove a file. Just files, there is a different command to remove folders, which we will get to in a bit. in order to remove a file you must use the rm command followed by the path to the unwanted file. An example would be "rm /home/user01/file.txt" or if you are in the current director where the file is located it would look like "rm file.txt" Simple no ?

rmdir: Remove Directory "rmdir [target directory]"

"rmdir" works similar to the rm command. One thing you need to be aware of is that if you are removing a directory say "rmdir /home/user01/Folder", Folder being the directory you wish to remove, it MUST be empty in order for the removal to be successful. Just an FYI.

zip: Zip archive program "zip -[0-9] [archive name] [file(s) to add]"

"zip" is a good way to combine files into an archive to either save space or just make things less cluttered. If you wanted to create and archive for a file called "myvideo.avi" it would look like this "zip -9 myzipfile.zip myvideo.avi". The "myzipfile.zip" is what you want to call your archive, and the file following that field are the file(s) you with to compress or store into a zip archive. Now notice the -9, what that means is to use the best compression possible. Using a -1 through -9 determines the compression strength. 1 being the lowest and 9 being the highest. If you choose to not compress at all you would use the -0 parameter to simple store the file(s).

unzip: Unzip a .zip archive "unzip [archive name] [specific file (optional)]"

"unzip" is quite easy to use. If you have a zip file you wish to uncompress/unpack there are a few ways to do it. A simple "unzip myzipfile.zip" will uncompress/extract its contents to your current directory. If the file contains say more then one file and you only want to extract a few files or just one it would look like the following, "unzip myzipfile.zip textfile01.txt". So say instead of extracting the files textfile01.txt through textfile08.txt it will only extract textfile01.txt

clear: Clear Screen

"clear" as for this command there is not much to it. Simply type "clear" in the command line and press Return and it will clear the screen give you a fresh terminal.

Other tips: The ampersand "&" function

You can launch your programs from a terminal window. Say you would like to launch the VLC Media Player. If you simply type "vlc" into a terminal and press return, it will launch the program. However, that terminal can not be use because it's focus is running the "vlc" program. An if you close the terminal it will close "vlc". So what can you do ? If you add an ampersand "&" after a program it will cause the program to run in the background and you get your terminal back. So the command should look like this "vlc &". It's that simple.

Well that about wraps up "Part 1" on using the Linux CLI. I will go into more depth about other programs and parameters in the next installment. So take care everyone, I hope this had helped you.

And always embrace the power of the CLI!

No comments:

Post a Comment