UNIX – Lesson 005 – wc, cp, mv and rm commands
The “wc” command displays a count of lines, words and characters in a file
wc [-n] filename
- -l Count lines
- -m Count characters
- -w Count words
- -c Count bytes
$ 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
$ wc –w ~/.cshrc
$ wc –m ~/.cshrc
$ wc –c ~/.cshrc
The “cp” command copies files or directories
cp [-i] filename new_filename
cp [-i] filename [ filename(s) …] dest_directory
cp –r [-i] directory [ directories] dest_directory
- -i Interactive mode: cp will prompt for confirmation if the copy would overwrite an existing target
- -r Recursively, used for directories
$ 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
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
The “mv” command moves or renames files or directories
mv [-if] filename new_filename
mv [-f] filename [ filename(s) …] dest_directory
mv [-i] directory [ directories] dest_directory
- -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.
$ 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
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
The “rm” command removes files or directories
rm [-if] filename [filename(s) …]
rm -r[-if] directory [ directories]
- -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.
$ rm –i Report.log
rm: remove Report.log (yes/no)?
$ rm –r Reports