Chapter 2. File Commands

Basic File Operations

ls

List files in a directory.

cp

Copy a file.

mv

Move (rename) a file.

rm

Remove (delete) a file.

ln

Create links (alternative names) to a file.

One of the first things you’ll do on a Linux system is manipulate files: copying, renaming, deleting, and so forth.

ls

stdin

stdout

- file

-- opt

--help

--version

ls [options] [files]

The ls command (pronounced as it is spelled, ell ess) lists attributes of files and directories. You can list files in the current directory:

ls

in given directories:

ls dir1 dir2 dir3

or individually:

ls myfile myfile2 myfile3
Tip

If ls behaves differently than you expect, your distro may have defined an alias for ls (see “Aliases”). Check for an alias by running this command:

alias ls
alias ls='/bin/ls -FHN'     Yes, there's an alias

To run the original command rather than the alias, prepend a backslash (\ls). To remove the alias, run unalias ls. Then look in your shell configuration files for the alias definition (see “Tailoring Shell Behavior”) and remove it. If you don’t see a definition, either add the command unalias ls or define a new alias that works the way you want, like alias ls="/bin/ls".

The most important ls options are -a, -l, and -d. By default, ls hides files whose names begin with a dot, as explained in the sidebar “Dot Files”. The -a option displays all files. Depending on your account settings, ls will list dot files at the beginning (sorting based on the dot) ...

Get Linux Pocket Guide, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.