As we’re working on command line, there are a lot of basic and useful commands that we need to know about. Please note that all commands are case sensitive.
sudo <command>
sudo means execute the following command as root user, which is equivalent to run as administrator in Windows. Root user is the user with the top privilege in linux. There are many commands that can only be executed by root user, the editing of many files also requires root privilege.
If you execute “sudo su”, you’re switching to root user and all commands are executed as root. You can exit root user by command “exit”.
halt
halt is the same as shutdown. This command requires sudo. To boot Raspberry Pi again, unplug the power and plug it back in.
reboot
reboot is the same as restart. This command also requires sudo.
man <command>
man is short for manual. It can show all usages and options and the explanation of a specific command.
Also, “-h” or “–help” option of a command would normally output a short summary of the manual.
Files and Directories
ls <path>
ls is short for list files, under current directory by default if no path is given. In Linux, file names are case sensitive. Also a directory is considered as a file, most files don’t have a extension.
Linux treats all files and directories have the name starts with . (a single dot) as hidden files/directories. If you would like to see the hidden files, use “ls -a”.
cd <path>
cd is short for change directory. We’re moving from one directory to another. Path can be type in absolute path (full path) or relative path (under current directory). If the path starts with /, it’s considered to be an absolute path. Otherwise it’s a relative path.
For long directory name, you can type in the first few characters, and hit tab, the system would automatically fill out the rest for you. If there are multiple matches, all matches will be listed and you can type in the one you want. This also works on flies name and some commands.
There are a few more expressions about path and directory in linux:
- . (a single dot) represents current directory.
- .. (two dots) represents the parent directory of the current directory.
- / (forward slash) represents the top directory. It doesn’t have a parent directory.
- ~ (tilde) represents the home directory of the current user.
pwd
pwd is short for print working directory. It will output the absolute path of your current location.
mkdir <path>
mkdir is short for make directory. The path can be either absolute path or relative path. However you cannot make a directory that has the same name with another file or directory, under the same parent directory.
rmdir <path>
rmdir is short for remove directory. Again the path can be absolute of relative. Unlike other systems, you can only do rmdir on empty directory. If there are other files in the directory and you would like to delete them all, use “rm -r”.
cp <source files> <destination path>
cp means copy. Copy the source files to the destination. If you would like to copy a directory, use “cp -r”.
Multiple source files are accepted, and the last input is always the destination. * (wildcard) can also be used to select all matched files.
mv <source files> <destination path>
mv means move. Move the source files to the destination. If you would like to move a directory, use “mv -r”.
rm <files>
rm means remove files, or delete files. To delete a directory, use “rm -r”. This works on a empty directory and a directory contains files. Note that there’s no trash on command line and all delete are permanent and cannot undo.
chown <owner:group> <files>
chown means change owner. Each file has an owner and a file group, as well as access permission control. Change the owner of a file would allow the new owner takes control of the file. root user can usually override the access control settings and change any files.
If you change the owner for a directory, it would only apply to the directory itself. If you would also like to change the owner for all files under it, use “chown -r”.
chmod <permission> <files>
chmod means change access permission. Access permission is a three digit number and each digit is range 0-7. The first digit is the owner’s permission. Second digit is the file group users’ permission. And the last digit represents all other users’ permission. There are three permissions, read permission is notated by number 4, write permission is number 2 and execute permission is number 1. Where multiple permission is gained, the corresponded numbers add up.
If you change access permission for a directory, it would only apply to the directory itself. If you would also like to change the access permission to all files under it, use “chmod -r”.
-r/-R option
-r/-R means recursive. Recursion means to execute the command not only to the directory itself, but also to all files under it. Some commands require capital and some others use lowercase.
Read and Edit Files
cat <files>
cat is to output the content of a file, or read the file.
head <files>
head is similar to cat. However it outputs only the first few lines of the file.
tail <files>
tail is also similar to cat, but it outputs the bottom few lines only.
grep <pattern> <files>
grep is to search for a specific string (pattern) in the input file. Only the lines that contain given string will be outputted.
nano <files>
nano is a basic and simple editor for binary files. It can either edit an existing file or create a new file if the input file doesn’t exist. The ^ symbol for keyboard command is control key.
There are also other editors pre-installed, such as vim. But nano is always the easiest to use.
These are the basic commands we’re interested in at the moment. As we explore, we will be using more command to speed up our work. We can now start to work on softwares and packages.