Skip to content

Commit 6bf555c

Browse files
examples added for mkdir and touch commands
1 parent ddbfce0 commit 6bf555c

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

Files_and_Directories.md

+64-13
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ ch.sh ip.txt power.log report.log workshop_brochures/
176176

177177
* long listing format
178178
* shows details like file permissions, ownership, size, timestamp, etc
179+
* See [chmod](./Working_with_Files_and_Directories.md#chmod) section for details on permissions, groups, etc
179180
* file types are distinguished as `d` for directories, `-` for regular files, `l` for symbolic links, etc
180181

181182
```bash
@@ -323,29 +324,77 @@ backups/ projects/ workshop_brochures/
323324

324325
>make directories
325326
326-
File names can use any character other than `/` and ASCII NUL character
327+
* Linux filenames can use any character other than `/` and the ASCII NUL character
328+
* quote the arguments if name contains characters like space, `*`, etc to prevent shell interpretation
329+
* shell considers space as argument separator, `*` is a globbing character, etc
330+
* unless otherwise needed, try to use only alphabets, numbers and underscores for filenames
327331

328-
**Examples**
332+
```bash
333+
$ # one or more absolute/relative paths can be given to create directories
334+
$ mkdir reports 'low power adders'
335+
336+
$ # listing can be confusing when filename contains characters like space
337+
$ ls
338+
low power adders reports
339+
$ ls -1
340+
low power adders
341+
reports
342+
```
343+
344+
* use `-p` option to create multiple directory hierarchies in one go
345+
* it is also useful in scripts to create a directory without having to check if it already exists
346+
* special variable `$?` gives exit status of last executed command
347+
* `0` indicates success and other values indicate some kind of failure
348+
* see documentation of respective commands for details
349+
350+
```bash
351+
$ mkdir reports
352+
mkdir: cannot create directory ‘reports’: File exists
353+
$ echo $?
354+
1
355+
$ # when -p is used, mkdir won't give an error if directory already exists
356+
$ mkdir -p reports
357+
$ echo $?
358+
0
359+
360+
$ # error because 'a/b' doesn't exist
361+
$ mkdir a/b/c
362+
mkdir: cannot create directory ‘a/b/c’: No such file or directory
363+
$ # with -p, any non-existing directory will be created as well
364+
$ mkdir -p a/b/c
365+
$ ls -1R a
366+
a:
367+
b
368+
369+
a/b:
370+
c
371+
372+
a/b/c:
373+
```
374+
375+
**Further Reading**
329376

330-
* `mkdir project_adder` create folder project_adder in current directory
331-
* `mkdir project_adder/report` create folder report in project_adder directory
332-
* `mkdir -p project_adder/report` create both project_adder and report directories in one shot
333-
* if project_adder already exists, it won't be affected
334-
* `mkdir /home/guest1` add a home directory for user guest1
335377
* [mkdir Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/mkdir?sort=votes&pageSize=15)
336378
* [mkdir Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/mkdir?sort=votes&pageSize=15)
379+
* [unix.stackexchange: Characters best avoided in filenames](https://unix.stackexchange.com/questions/269093/characters-best-avoided-in-filenames-when-used-in-bash-e-g)
337380

338381
<br>
339382

340383
## <a name="touch"></a>touch
341384

342-
>change file timestamps
385+
* Usually files are created using a text editor or by redirecting output of a command to a file
386+
* But sometimes, for example to test file renaming, creating empty files comes in handy
387+
* the `touch` command is primarily used to change timestamp of a file (see [touch](./Working_with_Files_and_Directories.md#touch) section of next chapter)
388+
* if a filename given to `touch` doesn't exist, an empty file gets created with current timestamp
343389

344-
When a filename is passed as argument to `touch` command that doesn't exist, it creates an empty file
345-
More info on this command is covered in a later chapter
346-
347-
* `touch error.log` creates an empty file error.log in current directory if it doesn't exist
348-
* [touch Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/touch?sort=votes&pageSize=15)
390+
```bash
391+
$ touch ip.txt
392+
$ ls -1F
393+
a/
394+
ip.txt
395+
low power adders/
396+
reports/
397+
```
349398

350399
<br>
351400

@@ -367,6 +416,7 @@ More info on this command is covered in a later chapter
367416
* `rm -d project_tmp` remove project_tmp folder provided it is empty
368417
* `rmdir project_tmp` can also be used
369418
* If available, use `gvfs-trash` command to send items to trash instead of permanent deletion
419+
* or, [unix.stackexchange: creating a simple trash command](https://unix.stackexchange.com/questions/452496/create-a-recycle-bin-feature-without-using-functions)
370420
* Files removed using `rm` can still be recovered with time/skill. Use `shred` command to overwrite files
371421
* [recover deleted files](https://unix.stackexchange.com/questions/80270/unix-linux-undelete-recover-deleted-files)
372422
* [recovering accidentally deleted files](https://unix.stackexchange.com/questions/2677/recovering-accidentally-deleted-files)
@@ -443,6 +493,7 @@ Note: The `perl` based `rename` is presented here and different from [util-linux
443493
* `rename 's/\.JPG$/.jpg/' *JPG` change the file extension from '.JPG' to '.jpg'
444494
* `rename 's/ /_/g' *` replace all 'space' characters in filenames with '_'
445495
* [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
446497

447498
<br>
448499

0 commit comments

Comments
 (0)