Skip to content

Commit c530904

Browse files
examples for rm, cp, mv and rename commands
1 parent 6bf555c commit c530904

File tree

1 file changed

+146
-47
lines changed

1 file changed

+146
-47
lines changed

Files_and_Directories.md

+146-47
Original file line numberDiff line numberDiff line change
@@ -402,25 +402,58 @@ reports/
402402

403403
>remove files and directories
404404
405-
**Options**
405+
* to delete files, specify them as separate arguments
406+
* to delete directories as well, use `-r` option (deletes recursively)
407+
* use `-f` option to force remove without prompt for non-existing files and write protected files (provided user has appropriate permissions)
406408

407-
* `-r` remove recursively, used for removing directories
408-
* `-f` force remove without prompt for non-existing files and write protected files (provided user has appropriate permissions)
409-
* `-i` prompt before every removal
410-
* `-d` remove empty directories
409+
```bash
410+
$ ls
411+
a ip.txt low power adders reports
412+
$ rm ip.txt
413+
$ ls
414+
a low power adders reports
411415

412-
**Examples**
416+
$ rm reports
417+
rm: cannot remove 'reports': Is a directory
418+
$ rm -r reports
419+
$ ls
420+
a low power adders
421+
422+
$ # to remove only empty directory, same as 'rmdir' command
423+
$ rm -d a
424+
rm: cannot remove 'a': Directory not empty
425+
```
426+
427+
* typos like misplaced space, wrong glob, etc could wipe out files not intended for deletion
428+
* apart from having backups and snapshots, one could take some mitigating steps
429+
* using `-i` option to interactively delete each file
430+
* using `echo` as a dry run to see how the glob expands
431+
* using a trash command (see links below) instead of `rm`
413432

414-
* `rm project_adder/power.log` remove file power.log from project_adder directory
415-
* `rm -r project_adder` remove folder project_adder from current directory even if non-empty
416-
* `rm -d project_tmp` remove project_tmp folder provided it is empty
417-
* `rmdir project_tmp` can also be used
418-
* If available, use `gvfs-trash` command to send items to trash instead of permanent deletion
433+
```bash
434+
$ rm -ri 'low power adders'
435+
rm: remove directory 'low power adders'? n
436+
$ ls
437+
a low power adders
438+
439+
$ rm -ri a
440+
rm: descend into directory 'a'? y
441+
rm: descend into directory 'a/b'? y
442+
rm: remove directory 'a/b/c'? y
443+
rm: remove directory 'a/b'? y
444+
rm: remove directory 'a'? y
445+
$ ls
446+
low power adders
447+
```
448+
449+
**Further Reading**
450+
451+
* See if a trash command is available for your distro (for ex: `gvfs-trash` on Ubuntu) - this will send items to trash instead of deletion
419452
* or, [unix.stackexchange: creating a simple trash command](https://unix.stackexchange.com/questions/452496/create-a-recycle-bin-feature-without-using-functions)
420453
* Files removed using `rm` can still be recovered with time/skill. Use `shred` command to overwrite files
421-
* [recover deleted files](https://unix.stackexchange.com/questions/80270/unix-linux-undelete-recover-deleted-files)
422-
* [recovering accidentally deleted files](https://unix.stackexchange.com/questions/2677/recovering-accidentally-deleted-files)
423-
* [Securely wipe disk](https://wiki.archlinux.org/index.php/Securely_wipe_disk)
454+
* [unix.stackexchange: recover deleted files](https://unix.stackexchange.com/questions/80270/unix-linux-undelete-recover-deleted-files)
455+
* [unix.stackexchange: recovering accidentally deleted files](https://unix.stackexchange.com/questions/2677/recovering-accidentally-deleted-files)
456+
* [wiki.archlinux: Securely wipe disk](https://wiki.archlinux.org/index.php/Securely_wipe_disk)
424457
* [rm Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/rm?sort=votes&pageSize=15)
425458
* [rm Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/rm?sort=votes&pageSize=15)
426459

@@ -430,48 +463,107 @@ reports/
430463

431464
>copy files and directories
432465
433-
The destination path is always specified as the last argument. More than one source file/folder can be specified if destination is a directory
466+
* to copy a single file or directory, specify the source as first argument and destination as second argument
467+
* similar to `rm` command, use `-r` for directories
434468

435-
**Options**
469+
```bash
470+
$ # when destination is a directory, specified sources are placed inside that directory
471+
$ # recall that . is a relative path referring to current directory
472+
$ cp /usr/share/dict/words .
473+
$ ls
474+
low power adders words
436475

437-
* `-r` copy recursively, used for copying directories
438-
* `-i` prompt before overwriting
439-
* `-u` copy files only if newer than existing file in destination location or if file doesn't exist in destination
476+
$ cp /usr/share/dict .
477+
cp: omitting directory '/usr/share/dict'
478+
$ cp -r /usr/share/dict .
479+
$ ls -1F
480+
dict/
481+
low power adders/
482+
words
483+
```
440484

441-
**Examples**
485+
* often, we want to copy for the purpose of modifying it
486+
* in such cases, a different name can be given while specifying the destination
487+
* if the destination filename already exists, it will be overwritten (see options `-i` and `-n` to avoid this)
442488

443-
* `cp /home/raja/Raja_resume.doc Ravi_resume.doc` create a copy of file Raja_resume.doc as Ravi_resume.doc in your current directory
444-
* `cp /home/raja/Raja_resume.doc .` create a copy of file Raja_resume.doc in your current directory - name not changed in this case
445-
* `.` represents current directory and `..` represents one hierarchy above
446-
* `cp -r /home/guest1/proj_matlab ~/proj_matlab_bug_test` copy proj_matlab to your home directory as proj_matlab_bug_test
447-
* `cp report/output.log report/timing.log .` copy files output.log and timing.log to current directory
448-
* [cp Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/cp?sort=votes&pageSize=15)
449-
* [cp Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/cp?sort=votes&pageSize=15)
489+
```bash
490+
$ cp /usr/share/dict/words words_ref.txt
491+
$ cp -r /usr/share/dict word_lists
492+
493+
$ ls -1F
494+
dict/
495+
low power adders/
496+
word_lists/
497+
words
498+
words_ref.txt
499+
```
450500

451-
Also check out
501+
* multiple files and directories can be copied at once if the destination is a directory
502+
* using `-t` option, one could specify destination directory first followed by sources (this is helpful with `find` command and other cases)
452503

504+
```bash
505+
$ mkdir bkp_dot_files
506+
507+
$ # here, ~ will get expanded to user's home directory
508+
$ cp ~/.bashrc ~/.bash_profile bkp_dot_files/
509+
$ ls -A bkp_dot_files
510+
.bash_profile .bashrc
511+
```
512+
513+
* see `man cp` and `info cp` for more options and complete documentation
514+
* some notable options are
515+
* `-u` copy files from source only if they are newer than those in destination or if it doesn't exist in destination location
516+
* `-b` and `--backup` for back up options if file with same name already exists in destination location
517+
* `--preserve` option to copy files along with source file attributes like timestamp
518+
519+
**Further Reading**
520+
521+
* [cp Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/cp?sort=votes&pageSize=15)
522+
* [cp Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/cp?sort=votes&pageSize=15)
453523
* `rsync` a fast, versatile, remote (and local) file-copying tool
454-
* [rsync examples](https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories-on-a-vps)
455-
* [rsync Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/rsync?sort=votes&pageSize=15)
456-
* [rsync Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/rsync?sort=votes&pageSize=15)
524+
* [rsync examples](https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories-on-a-vps)
525+
* [rsync Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/rsync?sort=votes&pageSize=15)
526+
* [rsync Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/rsync?sort=votes&pageSize=15)
457527

458528
<br>
459529

460530
## <a name="mv"></a>mv
461531

462532
>move (rename) files
463533
464-
The destination path is always specified as the last argument. More than one source file/folder can be specified if destination is a directory
534+
* as name suggests, `mv` can move files from one location to another
535+
* if multiple files need to be moved, destination argument should be a directory (or specified using `-t` option)
536+
* unlike `rm` and `cp`, both files and directories have same syntax, no additional option required
537+
* use `-i` option to be prompted instead of overwriting file of same name in destination location
465538

466-
**Options**
539+
```bash
540+
$ ls
541+
bkp_dot_files dict low power adders word_lists words words_ref.txt
542+
$ mkdir backups
467543

468-
* `-f` don't prompt for overwriting and moving write protected files (provided user has appropriate permissions)
469-
* `-i` prompt before overwriting
544+
$ mv bkp_dot_files/ backups/
545+
$ ls -F
546+
backups/ dict/ low power adders/ word_lists/ words words_ref.txt
547+
$ ls -F backups/
548+
bkp_dot_files/
470549

471-
**Examples**
550+
$ mv dict words backups/
551+
$ ls -F
552+
backups/ low power adders/ word_lists/ words_ref.txt
553+
$ ls -F backups/
554+
bkp_dot_files/ dict/ words
555+
```
556+
557+
* like `cp` command, for single file/directory one can provide a different destination name
558+
559+
```bash
560+
$ mv backups/bkp_dot_files backups/dot_files
561+
$ ls -F backups/
562+
dict/ dot_files/ words
563+
```
564+
565+
**Further Reading**
472566

473-
* `mv project_adder project_lowpower_adder` rename file or folder
474-
* `mv power.log timing.log area.log project_multiplier/result/` move the specified files to result directory
475567
* [mv Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/mv?sort=votes&pageSize=15)
476568
* [mv Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/mv?sort=votes&pageSize=15)
477569

@@ -481,19 +573,26 @@ The destination path is always specified as the last argument. More than one sou
481573

482574
>renames multiple files
483575
484-
Note: The `perl` based `rename` is presented here and different from [util-linux-ng version](https://linux.die.net/man/1/rename). Check `man rename` for details
576+
Note: The `perl` based `rename` is presented here which is different from [util-linux-ng version](https://linux.die.net/man/1/rename). Check `man rename` for details
485577

486-
**Options**
487-
488-
* `-f` overwrite existing files
489-
* `-n` dry run without actually renaming files
578+
```bash
579+
$ ls
580+
backups low power adders word_lists words_ref.txt
581+
$ # here, the * glob will expand to all non-hidden files in current directory
582+
$ # -n option is for dry run, to see changes before actually renaming files
583+
$ # s/ /_/g means replace all space characters with _ character
584+
$ rename -n 's/ /_/g' *
585+
rename(low power adders, low_power_adders)
586+
587+
$ rename 's/ /_/g' *
588+
$ ls
589+
backups low_power_adders word_lists words_ref.txt
590+
```
490591

491-
**Examples**
592+
**Further Reading**
492593

493-
* `rename 's/\.JPG$/.jpg/' *JPG` change the file extension from '.JPG' to '.jpg'
494-
* `rename 's/ /_/g' *` replace all 'space' characters in filenames with '_'
495594
* [rename Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/rename?sort=votes&pageSize=15)
496-
* See [Perl one liners](https://github.com/learnbyexample/Command-line-text-processing/blob/master/perl_the_swiss_knife.md) for examples and details on Perl based substitution command
595+
* See [Perl one liners](https://github.com/learnbyexample/Command-line-text-processing/blob/master/perl_the_swiss_knife.md) for examples and details on Perl substitution command
497596

498597
<br>
499598

0 commit comments

Comments
 (0)