Home > Unix > UNIX – Lesson 005 – wc, cp, mv and rm commands

UNIX – Lesson 005 – wc, cp, mv and rm commands


wc
The “wc” command displays a count of lines, words and characters in a file
Syntax :
wc [-n] filename

Key argument:

  • -l     Count lines
  • -m   Count characters
  • -w    Count words
  • -c     Count bytes

Example:
$ wc ~/.cshrc
79 276 2459

in this example we have in “.cshrc” file 79 lines, 276 words and 2459 characters (we have 2459 bytes too, because 1 char is 1 byte)

$ wc –l ~/.cshrc
79

$ wc –w ~/.cshrc
276

$ wc –m ~/.cshrc
2459

$ wc –c ~/.cshrc
2459

================================================================================
cp
The “cp” command copies files or directories
Syntax :
cp [-i] filename new_filename
cp [-i] filename [ filename(s) …] dest_directory
cp –r [-i] directory [ directories] dest_directory

Key argument:

  • -i    Interactive mode: cp will prompt for confirmation if the copy would overwrite an existing target
  • -r   Recursively, used for directories

Example:

$ cp Report.log Report.log.bck
# To copy Report.log in Report.log.bck, at the end you have 2 identical files.

$ cp Report.log Report.log.bck Reports/
# To copy Report.log and Report.log.bck files inside Reports directory.

$ cp –r Reports Reports_Old
Note:
If the Reports_Old directory already exists, then this command copies the Report directory (and its subdirectories) inside Reports_Old, otherwise if you want copy the files/directories of Reports inside Reports_Old you have to execute this command:
cp –r Reports/* Reports_Old

================================================================================
mv
The “mv” command moves or renames files or directories
Syntax :
mv [-if] filename new_filename
mv [-f] filename [ filename(s) …] dest_directory
mv [-i] directory [ directories] dest_directory

Key argument:

  • -i   will prompt for confirmation if the move would overwrite an existing target.
  • -f   will move the file(s) without prompting even if it is writing over an existing target. Note that this is the default.

Example:

$ mv Report.log Report.log.bck
# If the second file exists then it is lost!

$ mv Report.log Reports/
# To move Report.log file inside Reports directory

$ mv Reports Reports_Old
Note:
If the Reports_Old directory already exists, then this command moves the Reports directory (and its subdirectories) inside Reports_Old directory, otherwise if you want move the files/directories of Reports inside Reports_Old you have to execute this command:
mv Reports/* Reports_Old

================================================================================
rm
The “rm” command removes files or directories

Syntax :
rm [-if] filename [filename(s) …]
rm -r[-if] directory [ directories]

Key argument:

  • -i   Interactive: rm prompts for confirmation before removing any files.
  • -f   Remove all files (whether write-protected or not) in a directory without prompting the user. In a write-protected directory, however, files are never removed (whatever their permissions are).
  • -r   Recursively remove directories and subdirectories in the argument list.

Example:

$ rm –i Report.log
rm: remove Report.log (yes/no)?

$ rm –r Reports

Categories: Unix
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: