Apr 19 2007

LCoTD: Basic File Operations

Published by georgegumpert at 12:18 pm under Linux Command of the Day, Linux Resources, Series ()

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

One of the first things you’ll need to do on a Linux system is manipulate files: copying, renaming, deleting, and so forth. There are a number of commands to perform these actions, and they are: ls (list files in a directory), cp (Copy a file), mv (rename (”move”) a file), rm (Delete (”remove”) a file), and ln (Create links (alternative names) to a file). In today’s installment, we will cover all of these commands

ls [options] [files]

The ls command (pronounced as it is spelled, ell ess) lists attributes of files and directories. It is part of coreutils, and exists in /bin (as do the rest of the commands in this installment).
You can list files in the current directory:

$ ls

in given directories:

$ ls dir1 dir2 dir3

or individually:

$ ls file1 file2 file3

The most important options are -a and -l. By default, ls hides files whose names begin with a dot; the -a option displays all files. The -l option produces a long listing:

-rw-r--r-- 1 george users 149 Sep 21 2004 my.data

that includes, from left to right: the file’s permissions (-rw-r–r–), owner (george), group (users), size (149 bytes), last modification date (Sep 21, 2004) and name. See LCoTD: The Filesystem (Part 3: Operating System Directories and File Protections) for more information on permissions.

Useful Options

  • -a: List all files, including those whose names begin with a dot.
  • -l: Long listing, including file attributes. Add the -h option (”human-readable”) to print file sizes in kilobytes, megabytes, and gigabytes, instead of bytes.
  • -F: Decorate certain filenames to be meaningful symbols, indicating their types. Appends “/” to directories, “*” to executables, “@” to symbolic links, “|” to named pipes, and “=” to sockets. These are just visual indicators for you, not part of the filenames!
  • -i: Prepend the inode numbers of the files.
  • -s: Prepend the size of the file in blocks, useful for sorting files by their size:
    $ ls -s | sort -n
  • -R: If listing a directory, list its contents recursively.
  • -d: If listing a directory, do not list its contents, just the directory itself.

cp [options] files (file|dir)

The cp command normally copies a file:

$ cp file file2

or copies multiple files into a directory:

code>$ cp file1 file2 file3 file4 dir

Using the -a or -R option, you can also recursively copy directories.

Useful Option

  • -p: Copy not only the file contents, but also the file’s permissions, timestamps, and if you have sufficient permission to do so, its owner and group. (Normally the copies will be owned by you, timestamped now, with permissions set by applying your umask to the original permissions.)
  • -a: Copy a directory recursively, preserving special files, permissions, symbolic links, and hard link relationships. This combines the options -R (recursively copy including special files), -p (permissions), and -d(links).
  • -i: Interactive mode. Ask before overwriting destination files.
  • -f: Force the copy. If a destination file exists, overwrite it uncoditionally.

mv [options] source target

The mv (move) command can rename a file:

$ mv file1 file2

or moves files and directories into a destination directory:

$ mv file1 file2 dir3 dir4 destination_directory

Useful Options

  • -i: Interactive mode. Ask before overwriting destination files.
  • -f: Force the move. If a destination file exists, overwrite it unconditionally.

rm [options] files|directories

The rm (remove) command can delete files:

$ rm file1 file2 file3

or recursively delete directories:

$ rm -r dir1 dir2

Useful Options

  • -i: Interactive mode. Ask before deleting each file.
  • -f: Force the deletion, ignoring any errors or warnings.
  • -r: Recursively remove a directory and its contents. Use with caution, especially if combined with the -f option.

ln [options] source target

A link is a reference to another file, created by the ln command. There are two kinds of links. A symbolic link refers to another file by its path, much like a Windows “shortcut” or a Macintosh “alias.”

$ ln -s myfile softlink

If you delete the original file, the now-dangling link will be invalid, pointing to a nonexistent file path. A hard link, on the other hand, is simply a second name for a physical file on disk (in tech talk, it points to the same inode). Deleting the original file does not invalidate the link.

$ ln myfile hardlink

Symbolic links can cross disk partitions, since they are just references to file paths; hard links cannot, since an inode on one disk has no meaning on another. Symbolic links can also point to directories, whereas hard links cannot…unless you are the superuser and use the -d option.

Useful Options

  • -s: Make a symbolic link. The default is a hard link.
  • -i: Interactive mode. Ask before overwriting destination files.
  • -f: Force the link. If a destination file exists, overwrite it unconditionally.
  • -d: Allow the superuser to create a hard link to a directory.

It’s easy to find out where a symbolic link points with either of these commands:

$ readlink linkname
$ls -l linkname

And that covers it for today! Come back tomorrow, when I’ll be covering directory operations.

Sphere: Related Content

If you found this article useful and use StumbleUpon, please give it a thumbs up so more people can read it! Thank you!

Please take the time to check out this thread and leave a comment letting me know what you would like to see from this site. It's still relatively young and trying to find its way- you can make design pitstop the resource you always wanted!

Leave a Reply