Home » Linux Basics » 03 - Basic Commands
3

File and Directory Commands

Knowing the uses of the the notes, file and directory commands in the Linux OS

The following are some basic directory oriented Linux commands, their meanings, and examples. Typing 'man command' at the prompt will give a list of 'minus' options that may be used to augment the basic command.

Directory Commands

  1. mkdir new_directory_name Make Directory - Creates a new directory with the specified name
  2. cd directory_path Change Directory to directory_name. If the path starts with a '/', an absolute path from the root is used. Otherwise, the relative path from the current directory is used. If no path is specified, the current directory changes to the user's home directory. When no directory name is given, the path goes to your "home" directory.
  3. rmdir directory_name Remove Directory - Deletes a directory. Directories that contain files cannot be deleted; they have to be emptied first
  4. pwd Present Working Directory - Displays the current directory.
  5. ls directory_path Lists the contents of the directory_path. If the path starts with a '/', an absolute path from the root is used. Otherwise, the path from the current director is used. If no path is specified, the current directory's contents are listed.

File Commands

  1. cp origin_files destination Copies the file specified from origin_files to destination path. These paths may be absolute or relative with a filename at the end or just filenames. Many filenames separated by a single space or directories may be specified after 'cp'; the contents of all files are copied to the trailing directory; existing files with the same name in the destination directory are overwritten.
  2. mv origin_file(s) destination The same as the previous command with one distinction - instead of copying files, 'mv' just moves the existing copy to the specified destination. It is similar to a cut and paste operation rather than a copy and paste operation.
  3. The find command is used to search for files and folders. The find command follows the syntax 'find path -name file_name'. A full search from path is initiated; all sub paths and sub directories are searched for file_name. Results may be filtered by date and other parameters using '-' options.
  4. locate file_or_directory_name Searches for a file or directory and returns a display of places where the file is found. A partial name or path to search may be specified
  5. rm file_name Deletes file_name

Note: The '-i' option is very useful while working with directories and files. Issuing this option after the command and before the parameters makes the OS reconfirm with the user before files are overwritten or deleted.

The following are working examples of several Linux directory and file commands. Play around with these; issue similar commands on your system to get a feel for the OS.

    [ LinuxUser ] ~$ ls
#myfile.txt#  dskfdsjkf   lsinfo.txt  myfile.txt   xxx.txt
Java          errors      myScr       myfile.txt~
backup.sh     errors.txt  myScript    newfile.txt
[ LinuxUser ] ~$ rm errors
[ LinuxUser ] ~$ rm -i errors.txt
rm: remove regular file `errors.txt'? y
[ LinuxUser ] ~$ rmdir Java
[ LinuxUser ] ~$ mkdir Java
[ LinuxUser ] ~$ mv lsinfo.txt Java/
[ LinuxUser ] ~$ find lsinfo.txt Java/
Java/
Java/lsinfo.txt
[ LinuxUser ] ~$ cp myScr myfile.txt
[ LinuxUser ] ~$ cp -i myScr xxx.txt
cp: overwrite `xxx.txt'? y
[ LinuxUser ] ~$ pwd
/home/LinuxUser
[ LinuxUser ] ~$ cd /usr
[ LinuxUser ] /usr$ cd
[ LinuxUser ] ~$ cd Java
[ LinuxUser ] ~/Java$
[ LinuxUser ] ~$