Now you should have managed to open a terminal shell and are ready to try the first Linux commands. Dont forget to press the Enter key after typing the command.
First of all you need to know how to logout from the shell (to disconnect from the remotely server). From the command line type:
$ logout
After you have successfully logout, login again and practice the following Linux commands:
ls - short for list
ls lists the files in the current working folder. This is probably the first command to try out.
Example 1. List the files (and subdirectories) in the current directory:
$ ls
Example 2. Lists what files are in the subdirectory Documents:
$ ls Documents
Example 3. Lists information about the file file1.cpp:
$ ls -l Documents/file1.cpp
Example 4. Lists files and subdirectories of tmp using a long listing format:
$ ls -l /tmp
Example 5. More options for ls command in manul page*:
$ man ls
pwd - short for print working directory
pwd prints the full path of the current (working) directory.
Example 6. Print current working directory:
$ pwd
cd - short for change directory
cd stands for change (working) directory and that's what it does.
Example 7. Move to the parent directory of the current directory:
$ cd ..
Example 8. Change into the folder Documents in your current working directory:
$ cd Documents
Example 9. Change into the directory video under the directory pub under the root /:
$ cd /pub/video
mkdir - makes directories
Mkdir makes a directory that can hold files and other subdirectories.
Example 10. Make a directory called homework under the current directory:
$ mkdir homework
Example 11. To make a long path, use -p option:
$ mkdir -p Documents/homework/dir1/dir2/dir3
In this case 3 new directories will be created: dir1, dir2, dir3.
Example 12. Make multiple directories in bash and other shells with {dir1,dir2}:
$ mkdir /usr/local/src/bash/{old,new,dist,bugs}
Example 13. Make directory with -v (verbose) option:
$ mkdir -v Documents/homework
Example 14. More options of mkdir in manual page*.
$ man mkdir
rm - delete files and folders, short for remove
Files are deleted with the command rm. There is no easy way to undelete these files. Like most programs rm supports -v (verbose).
Example 15. Remove the file lab1.cpp :
$ rm /home/user/Documents/homework/lab1.cpp
Example 16. To delete folders, use rm together with -f (Do not prompt for confirmation) and -r (recursively remove directory trees):
$ rm -rf /home/user/Documents/homework/
Example 17. More options of rm in manual page*.
$ man rm
cp - copy a file
cp makes a copy of a file. The last argument specifies the destination name. The arguments preceding it are the source file(s). When copying the original source file is left untouched. When the command would cause a file to be copied over a file that already exists, you may be asked to confirm the copying command. To enforce this confirmation behavior, you may want to use the -i option.
Example 18. Mak a backup copy of the file progr1.cpp to progr1.cpp.bak in the current directory:
$ cp progr1.cpp progr1.cpp.bak
Example 19. Make copies of the files progr2 and progr3 in the directory homework, which resides under the home directory (i.e., referred to by the ~):
$ cp progr2 progr3 ~/homework
Example 20. More options of cp in manual page*.
$ man cp
mv - move (or rename) a file
mv command moves or renames files. The last argument specifies the destination. When the destination includes a path or is the name of a directory, files with the same name as the source files are placed in that path/directory. In this case, any number of source files (to be moved) may precede the destination. In both cases, the original source file won't exist any more (except with their new names and possibly new locations). When the command would cause a file to overwrite a file that already exists, you may be asked to confirm the move command. To ensure this confirmation behavior, you may want to use the -i option with the move command.
Example 21. Restore the file from a backup copy. After this, the file progr1.cpp.bak will no longer exists. The new file progr1.cpp will end up in the current directory:
$ mv progr1.cpp.bak progr1.cpp
Example 22. Move files progr2.cpp and progr2.h into the directory homework, which resides under the home directory (i.e., referred to by the ~). There will no longer be files named progr2.cpp and progr2.h in the current directory:
$ mv progr2.cpp progr2.h ~/homework
Example 23. More options of mv in manual page*.
$ man mv
Using special characters
* matches any string
? matches any character
.. the parent directory
~ home directory
Example 24. List all files and directories from the directory Documents (located in the user home directory) which names begin with progr:
$ ls -l ~/Documents/progr*
Example 25. Remove all .cpp files, with the second letter a in the name, from the directory homework:
$ rm -f homework/?a*.cpp
Example 26. Move and rename the file progr1.cpp in the Documents directory, located one directory up from the current directory:
$ mv progr1.cpp ../Documents/progr1.cpp.bak
Example 27: Move to the user's home directory which is /home/username:
$ cd ~
Summary:
cd - change directory
cp - copy
ls -l - list files with information
man - manual pages
mkdir - make directory
mv - move
rm - remove file/dir
rmdir - remove an empty directory
rm -r - remove recursively
rm -i - interactiv remove
pwd - print working directory
* To quit the manual page press q.