OsGate.org Logo

Linux basic commands, part 1: files and directories - linux commands files directories

System System

Date 23.01.2011

Visits 4175

"The first time that we are using a linux system it's useful to know some basic commands in order to begin to use the command line. These commands can include for example operations on files and directories and in this article we will cover this."


In this first article of a series, we're going to analyze the most important command line programs used in the Linux and *BSD world.

This first article is about files and folder basics.

P.S.: All commands and options description have been taken from their man pages.

Files and folders

cat - concatenate files and print on the standard output

Basically you can use cat to visualize one or more files directly on the standard output of your terminal. It's a very quick way to show what a text file contains.

cat [options] file_name [file_name2] [file_name3] [#.]
Useful options:
  • -n, show the column with line numbers

cd - change working directory

The famous "cd" lets you to change the working directory.

cd directory_name
You can also use special wild card characters, such as ".." to move up of one level or "~" to change directly in your home directory. Without parameters "cd" change to your home directory.

There aren't options.

cp - copy files and directories

Cp is used to copy files and directories.

cp [options] source dest
cp [options] source_1 [source_2] [source_3] [#.] directory
Note that if you use a directory as source, you must use the "-r" option, which means to copy the directory recursively.

Useful options:

  • -b, make a backup of each existing destination file
  • -i, prompt before overwrite
  • -r, copy directories recursively
  • -u, copy only when the SOURCE file is newer than the destination file or when the destination file is missing

file - determine file type

When you want to know the type of a file, you can use this command, which simply prints the type of the file passed as arguments.

file [options] file_to_check
Useful options:
  • -i, causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say 'text/plain; charset=us-ascii' rather than 'ASCII text'.
  • -f namefile, read the names of the files to be examined from namefile (one per line) before the argument list.

find - search for files in a directory hierarchy

Find is a very useful command used when you want to search for some file or directories. It's very flexible, and lets you to do very different type of research, like the research per permissions or group id and you can also for example execute a command on each file that match search parameters.

find path [options] expression

A short example of find:

find . -iname *myfile*

This will find in the current directory, all the file that contain "myfile" in the name. Note that the case-insensitive option "-iname" is used.

Useful options:

  • -executable, matches files which are executable and directories which are searchable (in a file name resolution sense).
  • -group gname, file belongs to group gname (numeric group ID allowed).
  • -name pattern, base of file name (the path with the leading directories removed) matches shell pattern pattern.
  • -perm mode, file's permission bits are exactly mode (octal or symbolic).
  • -user uname, file is owned by user uname (numeric user ID allowed).

Please read the man page of find cause its plenty of very useful option that are too long to be explained here.

less - opposite of more

Please read "more" first.

From the man page "Less is a file visualizer similar to "more" but allows backwards movement and other features." So you can prefer this rather than "more".

less file_to_visualize

There aren't useful options.

ls - list directory contents

This command simply lists the content of a folder.

ls [options] path_1 [path_2] [path_3] [...]

Without options it will simply print the list of files in column.

For a better use of ls, use the -l -h (-lh) options which add to the normal file list more information, such as the permissions or the user who has created the file.

Useful options:

  • -l, use a long listing format
  • -h, with -l, print sizes in human readable format (e.g., 1K 234M 2G)
  • -a, do not ignore entries starting with . (show hidden files)
  • -t, sort by modification time
  • -r, reverse order while sorting

mkdir - make directories

When you want to create some directories, you need mkdir.

mkdir [options] directory_name

There aren't useful options.

more - file perusal filter for crt viewing

More is a file visualizer, and is a very common command, although you can prefer less which provide more features. When you visualize a file with more you are able only to scroll down.

more file_to_visualize

There aren't useful options.

mv - move (rename) files

With this command you can do two primary actions: move a file (cut) and rename a file.

mv [options] old_name new_name #rename
mv [options] old_path new_path #move (cut)

Useful options:

  • -b, make a backup of each existing destination file
  • -i, prompt before overwrite
  • -f, do not prompt before overwriting
  • -u, move only when the SOURCE file is newer than the destination file or when the destination file is missing
  • -v, explain what is being done

pwd - print name of current/working directory

When you are lost in a console or you don't know the path of where you are, you can use pwd which will print the name of the current working directory


There aren't useful options.

rm - remove files or directories

When you have to remove files or directories you can use "rm". You have to notice that as the man page says, "rm does not remove directory by default". So when you want to delete directories you must provide the recursive "-r" options.

rm [options] file_1 [file_2] [file_3] [...]

Useful options:

  • -r, remove directories and their contents recursively
  • -f, ignore nonexistent files, never prompt
  • -i, prompt before every removal

rmdir - remove empty directories

This command remove directory, but only when they are empty.

rmdir [options] dir_1 [dir_2] [...]

There aren't useful options.