Linux administration

Perform Basic File Management

Nguyen Hai Chau
Vietnam National University

Filesystem Objects

  • A file is a place to store data: a possibly-empty sequence of bytes
  • A directory is a collection of files and other directories
  • Directories are organized in a hierarchy, with the root directory at the top
  • The root directory is referred to as /

Directory and File Names

  • Files and directories are organized into a filesystem
  • Refer to files in directories and sub-directories by separating their names with /, for example:
/bin/ls
/usr/share/dict/words
/home/jeff/recipe
  • Paths to files either start at / (absolute) or from some 'current' directory

File Extensions

  • It's common to put an extension, beginning with a dot, on the end of a filename
  • The extension can indicate the type of the file:
Extension File type
.txt Text file
.gif Graphics Interchange Format image
.jpg Joint Photographic Experts Group image
.mp3 MPEG-2 Layer 3 audio
.gz Compressed file
.tar Unix 'tape archive' file
.tar.gz, .tgz Compressed archive file
  • On Unix and Linux, file extensions are just a convention
    • The kernel just treats them as a normal part of the name
    • A few programs use extensions to determine the type of a file

Going Back to Previous Directories

  • The pushd command takes you to another directory, like cd. But also saves the current directory, so that you can go back later
  • For example, to visit Fred's home directory, and then go back to where you started from:
$ pushd ˜fred
$ cd Work
$ ls
...
$ popd
  • popd takes you back to the directory where you last did pushd
  • dirs will list the directories you can pop back to

Filename Completion

  • Modern shells help you type the names of files and directories by completing partial names
  • Type the start of the name (enough to make it unambiguous) and press Tab
  • For an ambiguous name (there are several possible completions), the shell can list the options:
    • For Bash, type Tab twice in succession
    • For C shells, type Ctrl+D
  • Both of these shells will automatically escape spaces and special characters in the filenames

Wildcard Patterns

  • Use the symbol * to match any part of a filename:
$ ls *.txt
accounts.txt
letter.txt
report.txt
  • Just * produces the names of all files in the current directory
  • The wildcard ? matches exactly one character:
$ rm -v data.?
removing data.1
removing data.2
removing data.3
  • Note: wildcards are turned into filenames by the shell, so the program you pass them to can't tell that those names came from wildcard expansion

Copying Files with cp

  • Syntax: cp [options] source-file destination-file

  • Copy multiple files into a directory: cp files directory

  • Common options:

    • -f, force overwriting of destination files
    • -i, interactively prompt before overwriting files
    • -a, archive, copy the contents of directories recursively

Examples of cp

  • Copy /etc/smb.conf to the current directory:
$ cp /etc/smb.conf .
  • Create an identical copy of a directory called work, and call it work-backup:
$ cp -a work work-backup
  • Copy all the GIF and JPEG images in the current directory into images:
$ cp *.gif *.jpeg images/

Moving Files with mv

  • mv can rename files or directories, or move them to different directories
  • It is equivalent to copying and then deleting but is usually much faster
  • Options:
    • -f, force overwrite, even if target already exists
    • -i, ask user interactively before overwriting files
  • For example, to rename poetry.txt to poems.txt:
$ mv poetry.txt poems.txt
  • To move everything in the current directory somewhere else: $ mv * ˜/old-stuff/

Deleting Files with rm

  • rm deletes ('removes') the specified files
  • You must have write permission for the directory the file is in to remove it
  • Use carefully if you are logged in as root!
  • Options:
    • -f, delete write-protected files without prompting
    • -i, interactive — ask the user before deleting files
    • -r, recursively delete files and directories
  • For example, clean out everything in /tmp, without prompting to delete each file:
$ rm -rf /tmp/*

Deleting Files with Peculiar Names

  • Some files have names which make them hard to delete
  • Files that begin with a minus sign:
$ rm ./-filename
$ rm -- -filename
  • Files that contain peculiar characters — perhaps characters that you can't actually type on your keyboard: Write a wildcard pattern that matches only the name you want to delete:
$ rm -i ./name-with-funny-characters*
  • The ./ forces it to be in the current directory
  • Using the -i option to rm makes sure that you won't delete anything else by accident

Making Directories with mkdir

  • Syntax: mkdir directory-names
  • Options:
    • -p, create intervening parent directories if they don't already exist
    • -m mode, set the access permissions to mode
  • For example, create a directory called mystuff in your home directory with permissions so that only you can write, but eveyone can read it:
$ mkdir -m 755 ˜/mystuff
  • Create a directory tree in /tmp using one command with three subdirectories called one, two and three:
$ mkdir -p /tmp/one/two/three

Removing Directories with rmdir

  • rmdir deletes empty directories, so the files inside must be deleted first
  • For example, to delete the images directory:
$ rm images/*
$ rmdir images
  • For non-empty directories, use rm -r directory, note: dangerous command
  • The -p option to rmdir removes the complete path, if there are no other files and directories in it. These commands are equivalent:
$ rmdir -p a/b/c
$ rmdir a/b/c a/b

Identifying Types of Files

  • The data in files comes in various different formats (executable programs, text files, etc.)
  • The file command will try to identify the type of a file:
$ file /bin/bash
/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1,
dynamically linked (uses shared libs), stripped
  • It also provides extra information about some types of file
  • Useful to find out whether a program is actually a script:
$ file /usr/bin/zless
/usr/bin/zless: Bourne shell script text
  • If file doesn't know about a specific format, it will guess:
$ file /etc/passwd
/etc/passwd: ASCII text

Changing Timestamps with touch

  • Changes the access and modification times of files
  • Creates files that didn't already exist
  • Options:
    • -a, change only the access time
    • -m, change only the modification time
    • -t [YYYY]MMDDhhmm[.ss], set the timestamp of the file to the specified date and time. GNU touch has a -d option, which accepts times in a more flexible format
  • For example, change the time stamp on homework to January 20 2001, 5:59p.m.
$ touch -t 200101201759 homework

Exercise 1

  • a. Use cd to go to your home directory, and create a new directory there called dog.
  • b. Create another directory within that one called cat, and another within that called mouse.
  • c. Remove all three directories. You can either remove them one at a time, or all at once.
  • d. If you can delete directories with rm -r, what is the point of using rmdir for empty directories?
  • e. Try creating the dog/cat/mouse directory structure with a single command.

Exercise 2

  • a. Copy the file /etc/passwd to your home directory, and then use cat to see what's in it.
  • b. Rename it to users using the mv command.
  • c. Make a directory called programs and copy everything from /bin into it.
  • d. Delete all the files in the programs directory.
  • e. Delete the empty programs directory and the users file.

Exercise 3

  • a. The touch command can be used to create new empty files. Try that now, picking a name for the new file:
$ touch baked-beans

b. Get details about the file using the ls command:

$ ls -l baked-beans
  • c. Wait for a minute, and then try the previous two steps again, and see what changes. What happens when we don't specify a time to touch?
  • d. Try setting the timestamp on the file to a value in the future.
  • e. When you're finished with it, delete the file.