Skip to content

Commit fabc922

Browse files
authoredJul 9, 2018
examples for ln command and other improvements
1 parent c530904 commit fabc922

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed
 

Diff for: ‎Files_and_Directories.md

+50-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<br>
1919

20-
Let's look at commonly used commands to navigate directories, create and modify files and folders. For certain commands, a list of commonly used options are also given
20+
Let's look at commonly used commands to navigate directories, create and modify files and directories. For certain commands, a list of commonly used options are also given
2121

2222
Make it a habit to use `man` command to read about a new command - for example `man ls`
2323

@@ -30,7 +30,7 @@ Short descriptions for commands are shown as quoted text (taken from `whatis` or
3030
>print name of current/working directory
3131
3232
* apart from knowing your current working directory, often used to copy the absolute path to be pasted elsewhere, like in a script
33-
* some Terminal emulators display the current directory path as window/tab title by default
33+
* some Terminal emulators display the current directory path as window/tab title
3434

3535
```bash
3636
$ pwd
@@ -116,7 +116,7 @@ $ pwd
116116

117117
>clear the terminal screen
118118
119-
You can also use `Ctrl+l` short-cut to clear the terminal screen (in addition, this retains any typed text)
119+
You can also use `Ctrl+l` short-cut to clear the Terminal screen (in addition, this retains any typed text)
120120

121121
<br>
122122

@@ -264,11 +264,11 @@ chrome_bookmarks_02_07_2018.html dot_files/
264264
Python_workshop_2017.pdf Scripting_course_2016.pdf
265265
```
266266

267-
* often, we want to prune which files/folders are to be listed
267+
* often, we want to prune which files/directories are to be listed
268268
* commands like `find` provide extensive features in this regard
269269
* the shell itself provides a matching technique called glob/wildcards
270270
* see [Shell wildcards](./Shell.md#wildcards) section for more examples and details
271-
* beginners incorrectly associate globbing with `ls` command, as a demonstration globbing results are shown using `echo` command first
271+
* beginners incorrectly associate globbing with `ls` command, so globbing results are shown below using `echo` command as a demonstration
272272

273273
```bash
274274
$ # all unquoted arguments are subjected to shell globbing interpretation
@@ -499,7 +499,7 @@ words_ref.txt
499499
```
500500

501501
* 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)
502+
* using `-t` option, one could specify destination directory first followed by sources (this is helpful with `find` command and other places)
503503

504504
```bash
505505
$ mkdir bkp_dot_files
@@ -555,6 +555,7 @@ bkp_dot_files/ dict/ words
555555
```
556556

557557
* like `cp` command, for single file/directory one can provide a different destination name
558+
* so, when source and destination has same parent directory, `mv` acts as renaming command
558559

559560
```bash
560561
$ mv backups/bkp_dot_files backups/dot_files
@@ -600,16 +601,52 @@ backups low_power_adders word_lists words_ref.txt
600601

601602
>make links between files
602603
603-
Create hard or soft link of file or folder. Soft link is similar to short-cuts created in Windows. Hard link is like same file with different name, same timestamp and permissions of original file. Hard links can be moved to another directory after creation, will still have content even when original file is deleted. On the other hand, soft links have their own timestamps and permissions, it cannot be moved to another folder unless the link creation was done using full path and of course becomes a dead link when original file is deleted. More differences [here](https://askubuntu.com/questions/108771/what-is-the-difference-between-a-hard-link-and-a-symbolic-link)
604+
* there are two types of links - symbolic and hard links
605+
* symbolic links is like a pointer/shortcut to another file or directory
606+
* if the original file is deleted or moved to another location, symbolic link will no longer work
607+
* if the symbolic link is moved to another location, it will still work if the link was done using absolute path (for relative path, it will depend on whether or not there's another file with same name in that location)
608+
* a symbolic link file has its own inode, permissions, timestamps, etc
609+
* most commands will work the same when original file or the symbolic file is given as command line argument, see their documentation for details
604610

605-
**Examples**
611+
```bash
612+
$ # similar to cp, a different name can be specified if needed
613+
$ ln -s /usr/share/dict/words .
614+
$ ls -F
615+
words@
616+
617+
$ # to know which file the link points to
618+
$ ls -l words
619+
lrwxrwxrwx 1 learnbyexample eg 21 Jul 9 13:41 words -> /usr/share/dict/words
620+
$ readlink words
621+
/usr/share/dict/words
622+
$ # the linked file may be another link
623+
$ # use -f option to get original file
624+
$ readlink -f words
625+
/usr/share/dict/english
626+
```
627+
628+
* hard link can only point to another file (not a directory, and restricted to within the same filesystem)
629+
* the `.` and `..` special directories are the exceptions, they are hard links which are automatically created
630+
* once a hard link is created, there is no distinction between the two files other than different filename/location - they have same inode, permissions, timestamps, etc
631+
* any of the hard link will continue working even if all the other hard links are deleted
632+
* if a hard link is moved to another location, the links will still be in sync - any change in one of them will be reflected in all the other links
633+
634+
```bash
635+
$ touch foo.txt
636+
$ ln foo.txt baz.txt
637+
$ ls -1i foo.txt baz.txt
638+
649140 baz.txt
639+
649140 foo.txt
640+
```
641+
642+
**Further Reading**
606643

607-
* `ln -s results/report.log .` create a symbolic link of report.log from results folder to current directory
608-
* `ln results/report.log report.log` create a hard link of report.log from results folder to current directory, will not lose content even if results/report.log file is deleted
609-
* `unlink report.log` delete link
610-
* `rm report.log` can also be used
644+
* `unlink` command to delete links (`rm` can be used as well)
611645
* [ln Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/ln?sort=votes&pageSize=15)
612646
* [ln Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/ln?sort=votes&pageSize=15)
647+
* [askubuntu: What is the difference between a hard link and a symbolic link?](https://askubuntu.com/questions/108771/what-is-the-difference-between-a-hard-link-and-a-symbolic-link)
648+
* [unix.stackexchange: What is the difference between symbolic and hard links?](https://unix.stackexchange.com/questions/9575/what-is-the-difference-between-symbolic-and-hard-links)
649+
* [unix.stackexchange: What is a Superblock, Inode, Dentry and a File?](https://unix.stackexchange.com/questions/4402/what-is-a-superblock-inode-dentry-and-a-file)
613650

614651
<br>
615652

@@ -622,7 +659,7 @@ Usually so often combined with compression utility like `gzip` that there is a w
622659

623660
Archive and Compression
624661

625-
* `tar -cvf backup_mar15.tar project results` create backup_mar15.tar of files/folders project and results
662+
* `tar -cvf backup_mar15.tar project results` create backup_mar15.tar of files/directories project and results
626663
* `-v` option stands for verbose, i.e displays all the files and directories being archived
627664
* `gzip backup_mar15.tar` overwrites backup_mar15.tar with backup_mar15.tar.gz, a compressed version
628665
* `tar -cvzf backup_mar15.tar.gz project results` create backup_mar15.tar and overwrite with backup_mar15.tar.gz

0 commit comments

Comments
 (0)
Please sign in to comment.