Linuxtopia - Red Hat Enterprise Linux Step by Step Guide -
Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ ">

4.5. Manipulating Files in Your Current Working Directory

Below is a summary of basic shell commands:

ActionCommandFormatDetails
Copy a filecpcp filename destination Copies the file filename to the location destination.
List Directory ContentslslsThe ls command lists files in the current working directory.
Move a filemvmv which_file destinationTo move a file from one directory to another, use mv.
Rename a filemvmv oldname newnameThe use of the mv command changes the name of the file from oldname to newname.

Table 4-2. Commands for Manipulating Files

Each of the commands listed above have options that may be specified on the command line. More information about each is provided below.

4.5.1. Viewing Directory Contents with ls

To see what is in your current working directory, use the ls command.

Many options are available with the ls command. The ls command, by itself, does not show all of the files in the directory. Some files are hidden files (also called dot files) and can only be seen with an additional option specified to the ls command.

TipTip
 

To view all ls command options, read the man page by entering man ls at a shell prompt. To print the man page, enter man ls | col -b | lpr at the prompt.

Enter the command ls -a. Now you an view the hidden "dot" files.

Viewing all the files using the ls -a command can give you plenty of detail, but you can view still more information by using multiple options.

To see the size of a file or directory, when it was created, and so on, add the long option (-l) to the ls -a command. This command shows the file creation date, its size, ownership, permissions, and more.

You do not have to be in the directory whose contents you want to view to use the ls command. For example, to see what is in the /etc/ directory from your home directory, type:

ls -al /etc

Figure 4-3. Sample ls Output for the /etc Directory

The following is a brief list of options commonly used with ls. Remember, you can view the full list by reading the ls man page (man ls).

  • -a (all) — Lists all files in the directory, including hidden files (.filename). The .. and . at the top of your list refer to the parent directory and the current directory, respectively.

  • -l (long) — Lists details about contents, including permissions (modes), owner, group, size, creation date, whether the file is a link to somewhere else on the system and where its link points.

  • -F (file type) — Adds a symbol to the end of each listing. These symbols include /, to indicate a directory; @, to indicate a symbolic link to another file; and *, to indicate an executable file.

  • -r (reverse) — Lists the contents of the directory in reverse sort order.

  • -R (recursive) — Lists the contents of all directories below the current directory recursively.

  • -S (size) — Sorts files by their sizes.

The ls command can also be used with wildcards to display information on files or directories that match a certain pattern. To list all files and directories that begin with the letter "a", enter the following command.

ls -al a*

Remember that Linux is case-sensitive. The above command will not display information on files that start with "A".

4.5.2. Copying files with cp

To create a copy of an existing file, use the cp command.

While cp does have options, they are not used as often as those of other commands. To view these options, read the man page by entering man cp at a shellprompt.

To copy a file within the current directorym specify the new name as the third wod on the command line.

cp original_file new_file

This command creates a new file, named new_file, with the same content as the original file.

To copy a file to a different direcoty, specify a path as the third word on the command line:

cp original_file /dir1/dir2/

This command creates a copy of original_file in dir2/. If the last part of the path is a filename instead of a directory, the copy has that new name.

cp original_file /dir1/dir2/new_file

This creates a new file named new_file with the contents of original_file in dir2/.

Alternatively, if you know where the file is and would like to place a copy of it in your current directory, enter the path as word two and "." as the third word.

cp /dir1/dir2/filename .

The above command places a copy of filename in your current working directory.

4.5.3. Moving files with mv

To move a file or directory from one location to another, use the command mv.

Common useful options for mv include:

  • -i (interactive) — Prompts you if the file you have selected overwrites an existing file in the destination directory.

  • -f (force) — Overrides the interactive mode and moves without prompting. Be very careful about using this option.

  • -v (verbose) — Shows the progress of the files as they are being moved.

To move a file from the current directory to another location, enter a path as the third word on the command line.

mv filename /dir1/

This command would remove filename from the current working directory and place it in /dir1/.

Alternatively, a path to the location of the file may be entered as the second word and "." as the thrid word. This moves the file from the location specified in word two into your current working directory.

mv /tmp/filename .

The above command moves the file filename from the /tmp/ directory into your current working directory.

Finally, both words two and three may be paths.

mv ../../filename /tmp/new_name

The command above moves the file filename from a directory two levels up to the /tmp/ directory while renaming the file new_name.

NoteNote
 

You can only move files to a directory that already exists: Linux will not create a new directory with the mv command.

4.5.4. Renaming files with mv

To rename a file or directory, use the mv command.

To rename a file with mv, the third word on the command line must end in the new filename.

mv original_name new_name

The above command renames the file original_name to new_name.

mv ../original_name new_name

The above command moves the file original_name from one directory up to the current directory and renames it new_name.

mv original_name /dir1/dir2/dir3/new_name

The above command moves the file original_name from the current working directory to directory dir3/ and renames it new_name.

4.5.5. Delete files with rm

CautionCaution
 

Deleting a file with rmis permanent — you cannot un-delete it.

To delete a file using rm enter the following at a shell prompt:

rm filename

The second word can also be a path, but must end in a file.

rm ../../filename

There are many options to rm. To view them all, enter man rm at the shell prompt.

  • -i (interactive) — Prompts you to confirm the deletion. This option can stop you from deleting a file by mistake.

  • -f (force) — Overrides interactive mode and removes the file(s) without prompting. This might not be a good idea, unless you know exactly what you are doing.

  • -v (verbose) — Shows the progress of the files as they are being removed.

  • -r (recursive) — Deletes a directory and all files and subdirectories it contains.

TipTip
 

The interactive, or -i, option for rm causes it to ask if you are sure before permanently deleting a file or directory. You can make this the default behavior for rm by editing the .bashrc file. This file is located in your home directory. You can edit the file with any text editor.

gedit /home/user/.bashrc

Add the following line somewhere in the file. The end of the file is a good location.

alias rm='rm -i'

Save the file. At the command line, enter the following command.

source .baschrc

Now you will be asked to enter [Y] before deleting a file or directory with rm

4.5.6. Deleting directories

There are two commands that can be used to delete directories. The first is rmdir and the second is rm.

The rmdir command will only delete directories that are empty. If you are concerned about accidentally deleting a directory that is not empty, use this command.

rmdir directory/

The above command permanently deletes directory/ if it is empty.

If you want to delete a directory and all of its contents, use the command rm -rf. Note that if you enter rm -rf, the shell will not ask if you are sure before permanently deleting the directory.

rm -rf /dir1/

The above command deletes /dir1/ and every file and sub-directory that exists inside.

 
 
  Published under the terms of the GNU General Public License Design by Interspire