Typically the learning curve of Linux commands is bit slow and scare people away from it. But the piece of advice that is worth is “Hang on!, You will see the fruits of your frustrations soon”.
Linux organizes everything in directories. For example, if you are a linux user, your account is just another directory. And inside each directory, you will have other directories. Often the most basic tasks in getting around the system involve creating a directory, changing to a directory, creating a file in a directory and so on.
Here are some basic linux/unix commands that lets you get started exploring the treasure beneath the terminal.
How to Find What is in a Directory?
Use the command “ls”, to find out what is inside a directory. “ls” basically means list and when you type “ls” from the current directory you are in, it will simply list the files and directories in that directory in alphabatic order.
Just like any other linux command, “ls” comes with loads of other options that you can use to get more information about the content in a directory.
For example, simply using “ls” does not give you information about the size of the files in the directory or the time it was created. To list the content of a directory with more options like mentioned above, use “ls” with the option “l”. The way to do it is
ls -l
This will result in a output containing each file/directory in a separate line. Without going into the details of the output, one can see that each line will tell you whether it is a file or directory, when was it created, who created it, size of the file/directory.
How to Create a New Directory?
If you are windows user, you might be calling directories as folders. The command to create a new directory is “mkdir”, which basically tells that “make directory”. For example, if you want to create a new directory named “Papers”, then type
mkdir Papers
How to Change or Move to a Directory
Often you might want to move (or change) to a directory from your home directory. The command to use to change the current working directory is “cd” and it basically means “change directory”. The way to use “cd” is to give the path to the new directory you want to change to. For example, if you are in your home directory and want to change to a directory named “Documents” that right in your home directory, you type
$cd Documents
This command will move you to the “Document” directory from your home directory.
How to Find the Type of a File?
Another common thing that one might to know is to view the content of an existing file. Before trying to view a file it is useful to know the type of the file, whether it is a text file or an image or a document like pdf. Typically, you will be able to tell the type of a file from the secondary name of a file. However, you might find yourself in a scenario you might more than that.
The command to find the type of a file using a terminal is “file”. Simply type “file” followed by the file name you will see information about the type of the file. For example,
$file vehile.pdf
will give you
$vehicle.pdf: PDF document, version 1.4
How to Rename or Move a File?
Another command one might need to use frequently is to rename an old file to a new file. The command to rename or move a file in command line in Linux or Mac OS X is “mv” (it basically means move). Here is how to use “mv” command to rename or move a file.
mv oldFile newFile
You can use “ls command before and after using the “mv” command and see what it has done.
How to Copy a File in Command Line?
The command to copy a file in command line in Linux or Mac OS X is “cp”, where “cp” basically means copy. Here is how to use “cp” command to copy a file.
cp oldFile newFile
A good practice while using “mv” or “cp” command is to use these commands with the option “-i”. The “-i” option will ask your permission to overwrite if the file you are exists already. And it helps you losing a potentially an important file. The way to use “mv” or “cp” with “-i” option is as follows
cp -i oldFile newFile
How to Remove a File?
Removing a file is definitely the important and powerful command in Linux or Mac OS X. The key to remember here is that, in general, if you remove a file it is gone for ever (unless it is backed up somewhere else). Exercise caution while removing a file. The command to remove a file using terminal is “rm”, which simply means “remove” just like other commands. A good practice is to use with “-i” option.
rm -i myFile