LPI Linux Certification/Perform Basic File Management

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Detailed Objective[edit | edit source]

(LPIC-1 Version 5.0)

Weight: 4

Description: Candidates should be able to use basic Linux commands to manage files and directories.

Key Knowledge Areas:

  • Copy, move and remove files and directories individually.
  • Copy multiple files and directories recursively.
  • Remove files and directories recursively.
  • Use simple and advanced wildcard specifications in commands.
  • Use find to locate and act on files based on type, size, or time.
  • Usage of tar, cpio and dd.

The following is a partial list of the used files, terms and utilities:

  • cp
  • find
  • mkdir
  • mv
  • ls
  • rm
  • rmdir
  • touch
  • tar
  • cpio
  • dd
  • file
  • gzip
  • gunzip
  • bzip2
  • xz
  • unxz
  • file globbing

Create and Remove directories[edit | edit source]

To create a directory, use mkdir.

mkdir [options] dir

Common options:

-m  mode: set permission mode. Default use umask.
-p  parent: create parent directory as needed.

Examples:

mkdir -m 0700 bin
mkdir -p bin/system/x86

To delete an empty directory, use rmdir.

rmdir [options] dir

Common options:

-p  parent: remove empty superdirectories.

Examples:

rmdir  tmp
rmdir -p bin/system/x86

Copy files and directories[edit | edit source]

To copy one file to another, or to a directory, use cp.

cp [options] source target

Source and target can be a file or a directory.

Common options:

-i  interactive: prompt to overwrite
-r  recursive: copy the subdirectories and contents. Use -R for special files.
-f  force: force the overwriting

The default is to silently clobber the target file. (This does not alter the source.

Examples:

cp *.[a-z] /tmp
cp readme readme.orig
cp  ls /bin
cp -ri bin/* /bin

Move & Rename files[edit | edit source]

To rename a file or directory or to move a file or directory to another location, use mv.

mv [options] source target

Source and target can be a file or a directory.

Common options:

-i  interactive: prompt to overwrite
-f  force: force the overwriting
-v  verbose

The default is to silently clobber the target file.

Examples:

mv *.[a-z] /tmp
mv readme readme.orig
mv ls /bin
mv -fi bin/* /bin

Listing filenames and information[edit | edit source]

The command to list files in the current directory is ls.

ls [options] [filenames]

Common options are:

-l For a long format
-F Append a file type character
-a All files, including hidden files
-R Recursive listing of subtree
-d Do not descend into directory

The ls is equivalent to the dir command on DOS.

Examples of ls output:

$ ls -l /bin/ls
-rwxr-xr-x    1   root  root  46784 mar 23  2002 /bin/ls
$ ls -ld /bin
drwxr-xr-x    2 root   root   2144 nov  5 11:55 /bin
$ ls -a .
.bash_history .bash_profile .bashrc ...
$ ls -dF /etc .bashrc /bin/ls
.bashrc  /bin/ls*  /etc/

File types[edit | edit source]

The long format means:

$ ls -l /etc/hosts    #List a long format of the file hosts
-rw-r—r-- 1 root root 677 Jul 5 22:18 /etc/hosts

File content and location Linux/Unix does not distinguish file by filename extension, like Windows. To determine the file content use file.

$ file /etc .bashrc /bin/ls /dev/cdrom
/etc:       directory
.bashrc:    ASCII English text
/bin/ls:    ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped
/dev/cdrom: symbolic link to /dev/hdc

To determine if a command is a built-in shell command or a program, use type, and use which to find its location.

$ type cp cd ls which type
cp is /bin/cp
cd is a shell builtin
ls is aliased to `ls $LS_OPTIONS'
which is aliased to `type -p'
type is a shell builtin
$ which cut
/usr/bin/cut

Creating and using filenames[edit | edit source]

Filenames can be created with:

  • I/O redirection
cat chapter1 chapter2 > book
  • An editor, such as vi.
vi mynewfile
  • Many of the Unix utilities
cp file newfile
  • An application
netscape
  • The touch command, which creates empty files (or updates the "date modified" of existing files)
touch memo

The valid filename may have (or be):

  • Maximum 255 characters per filename
  • Any character except forward '/'
  • Recommended alphanumeric characters as well as plus, minus, and underscore characters.
  • Case sensitive ('A' and 'a' are treated differently)

Characters to avoid

  • Hyphen character.
touch my-file -lt
  • White space.
touch more drink
touch "more drink"
  • Most other special characters !@#$%^&*():;"'}{|\<,>.?~`
touch memo*

Remove files or directories[edit | edit source]

To remove files or subtree directories, use rm.

rm [options] files

Files can be a file or a directory.

Common options:

-i  interactive: prompt for each removal
-f  force: force the overwriting
-r  recursive: remove subtree directories and contents

There is no 'unremove' or 'undelete' command.

Examples:

rm *.[a-z]
rm readme readme.orig
rm  ls /bin
rm -rfi /bin
cd; rm -rf *  .* # This removes all files in the home directory of the current user, as well as those in the subdirectories therein!

Locating files in a subtree directory[edit | edit source]

To search for a file in a subtree directory, use find.

find [subtrees] [conditions] [actions]

The command can take multiple conditions and will search recursively in the subtree.

Some possible conditions are:

-name [FNG]  # Search for the FNG name
-type c      # Type of file [bcdfl]
-size [+-]#  # Has a +- size in blocks (c: bytes, k: kilobytes)
-user [name] # Own by user
-atime [+-]# # Accessed days ago.  +n means the file has not been accessed for the last n days.  -n means the file has been accessed in the last n days.
-mtime [+-]# # Modified days ago
-perm nnn    # Has permision flags nnn 

Some possible actions are:

-print  # Print the pathname
-exec cmd {} \; # Execute cmd on the file
-ok cmd {} \;   # Same as -exec but ask first

Examples:

find . -name '*.[ch]' -print
find /var /tmp . -size +20 -print
find ~ -type c -name '*sys*' -print
find / -type f -size +2c -exec rm -i {} \;
find / -atime -3 -print
find ~jo ~toto -user chloe -exec mv {} /tmp \;

To locate a binary, source file, or man page, use whereis.

whereis [options]

Common options:

-b: Search only for binaries.
-m: Search only for manual sections.
-s: Search only for sources.

Examples:

$ whereis host
host: /usr/bin/host /etc/host.conf /usr/share/man/man1/host.1.gz
$ whereis -m host
host: /usr/share/man/man1/host.1.gz

To locate a file located somewhere defined by the PATH variable, use which.

$ which -a ls
/bin/ls

The -a will look for all possible matches in PATH, not just for the first one.

Exercises[edit | edit source]

  1. Compose an interactive command to remove all .tmp files in your home directory. Respond y to every prompt.
  2. List all the files in the user's home directories ending with .pdf that are bigger than 50 blocks and have not been accessed for a month.
  3. Create a file file.h that will contain all the filenames ending with .h found in the /usr directory.
  4. Do a touch on all the c files found in /usr/src/packages directory.
  5. What are the default permissions when you create a new file and a new directory?
  6. How would you create a new file or directory that contains a space in the filename? (Example: 'new dir')
  7. What is the command to remove all the files of types char and block in your home directory?
  8. How would you find the location of the program find?
  9. Delete all files in /tmp which are not owned by root and have not been accessed for a week.