@@ -124,47 +124,207 @@ You can also use `Ctrl+l` short-cut to clear the terminal screen (in addition, t
124
124
125
125
> list directory contents
126
126
127
- ** Options **
127
+ * by default, ` ls ` output is sorted alphabetically
128
128
129
- * ` -a ` list hidden files also
130
- * ` -A ` like ` -a ` but excluding ` . ` and ` .. `
131
- * ` -1 ` list in single column (number one, not lowercase of letter L)
132
- * ` -l ` list contents with extra details about the files (lowercase of letter L, not number one)
133
- * ` -h ` display file sizes in human readable format
134
- * ` -t ` sort based on time
135
- * ` -r ` reverse sorting order
136
- * ` -R ` recursively display sub-directories
137
- * ` -S ` sort by file size
138
- * directory is treated as file and doesn’t display actual size used by directory, use ` du ` command if directory size is also needed
139
- * ` -d ` list directory entries instead of contents
140
- * ` -q ` prints ? instead of non-graphic characters like ` \n ` (Linux file names can use any character other than ` / ` and null character)
141
- * ` -F ` Append a character to each file name indicating the file type (other than regular files)
129
+ ``` bash
130
+ $ # if no argument is given, current directory contents are displayed
131
+ $ ls
132
+ backups hello_world.py palindrome.py projects todo
133
+ ch.sh ip.txt power.log report.log workshop_brochures
134
+
135
+ $ # absolute/relative paths can be given as arguments
136
+ $ ls /var/
137
+ backups crash local log metrics run spool
138
+ cache lib lock mail opt snap tmp
139
+ $ # for multiple arguments, listing is organized by directory
140
+ $ ls workshop_brochures/ backups/
141
+ backups:
142
+ chrome_bookmarks_02_07_2018.html dot_files
143
+
144
+ workshop_brochures:
145
+ Python_workshop_2017.pdf Scripting_course_2016.pdf
146
+
147
+ $ # single column listing
148
+ $ ls -1 backups/
149
+ chrome_bookmarks_02_07_2018.html
150
+ dot_files
151
+ ```
152
+
153
+ * ` -F ` appends a character to each file name indicating the file type (other than regular files)
142
154
* ` / ` for directories
143
155
* ` * ` for executable files
144
156
* ` @ ` for symbolic links
145
157
* ` | ` for FIFOs
146
158
* ` = ` for sockets
147
159
* ` > ` for doors
148
160
* the indicator details are described in ` info ls ` , not in ` man ls `
149
- * ` --color=auto ` list contents with different color for directories, executables, etc
150
161
151
- ** Examples**
162
+ ``` bash
163
+ $ ls -F
164
+ backups/ hello_world.py* palindrome.py* projects@ todo
165
+ ch.sh* ip.txt power.log report.log workshop_brochures/
166
+
167
+ $ # if you just need to distinguish file and directory, use -p
168
+ $ ls -p
169
+ backups/ hello_world.py palindrome.py projects todo
170
+ ch.sh ip.txt power.log report.log workshop_brochures/
171
+ ```
172
+
173
+ * or use the color option
174
+
175
+ ![ ls color output] ( ./images/ls_color.png )
176
+
177
+ * long listing format
178
+ * shows details like file permissions, ownership, size, timestamp, etc
179
+ * file types are distinguished as ` d ` for directories, ` - ` for regular files, ` l ` for symbolic links, etc
152
180
153
- * ` ls ` list contents of current directory when argument is not given
154
- * ` ls /home ` list contents of directory home present under the root directory (absolute path specified)
155
- * ` ls ../ ` list contents of directory one hierarchy above (relative path specified)
156
- * ` ls -ltr ` list files of current directory with details sorted such that latest created/modified file is displayed last
181
+ ``` bash
182
+ $ ls -l
183
+ total 84
184
+ drwxrwxr-x 3 learnbyexample eg 4096 Jul 4 18:23 backups
185
+ -rwxr-xr-x 1 learnbyexample eg 2746 Mar 30 11:38 ch.sh
186
+ -rwxrwxr-x 1 learnbyexample eg 41 Aug 21 2017 hello_world.py
187
+ -rw-rw-r-- 1 learnbyexample eg 34 Jul 4 09:01 ip.txt
188
+ -rwxrwxr-x 1 learnbyexample eg 1236 Aug 21 2017 palindrome.py
189
+ -rw-r--r-- 1 learnbyexample eg 10449 Mar 8 2017 power.log
190
+ lrwxrwxrwx 1 learnbyexample eg 12 Jun 21 12:08 projects -> ../projects/
191
+ -rw-rw-r-- 1 learnbyexample eg 39120 Feb 25 2017 report.log
192
+ -rw-rw-r-- 1 learnbyexample eg 5987 Apr 11 11:06 todo
193
+ drwxrwxr-x 2 learnbyexample eg 4096 Jul 5 12:05 workshop_brochures
194
+
195
+ $ # to show size in human readable format instead of byte count
196
+ $ ls -lh power.log
197
+ -rw-r--r-- 1 learnbyexample eg 11K Mar 8 2017 power.log
198
+
199
+ $ # use -s option instead of -l if only size info is needed
200
+ $ ls -1sh power.log report.log
201
+ 12K power.log
202
+ 40K report.log
203
+ ```
204
+
205
+ * changing sorting criteria
206
+ * use ` -t ` to sort by timestamp, often combined with ` -r ` to reverse the order so that most recently modified file shows as last item
207
+ * ` -S ` option sorts by file size (not suitable for directories)
208
+ * ` -v ` option does version sorting (suitable for filenames with numbers in them)
209
+ * ` -X ` option allows to sort by file extension (i.e characters after the last ` . ` in filename)
210
+
211
+ ``` bash
212
+ $ ls -lhtr
213
+ total 84K
214
+ -rw-rw-r-- 1 learnbyexample eg 39K Feb 25 2017 report.log
215
+ -rw-r--r-- 1 learnbyexample eg 11K Mar 8 2017 power.log
216
+ -rwxrwxr-x 1 learnbyexample eg 1.3K Aug 21 2017 palindrome.py
217
+ -rwxrwxr-x 1 learnbyexample eg 41 Aug 21 2017 hello_world.py
218
+ -rwxr-xr-x 1 learnbyexample eg 2.7K Mar 30 11:38 ch.sh
219
+ -rw-rw-r-- 1 learnbyexample eg 5.9K Apr 11 11:06 todo
220
+ lrwxrwxrwx 1 learnbyexample eg 12 Jun 21 12:08 projects -> ../projects/
221
+ -rw-rw-r-- 1 learnbyexample eg 34 Jul 4 09:01 ip.txt
222
+ drwxrwxr-x 3 learnbyexample eg 4.0K Jul 4 18:23 backups
223
+ drwxrwxr-x 2 learnbyexample eg 4.0K Jul 5 12:05 workshop_brochures
224
+
225
+ $ ls -X
226
+ backups todo power.log hello_world.py ch.sh
227
+ projects workshop_brochures report.log palindrome.py ip.txt
228
+ ```
229
+
230
+ * filenames starting with ` . ` are considered as hidden files
231
+
232
+ ``` bash
233
+ $ # -a option will show hidden files too
234
+ $ ls -a backups/dot_files/
235
+ . .. .bashrc .inputrc .vimrc
236
+
237
+ $ # . and .. are special directories pointing to current and parent directory
238
+ $ # if you recall, we have used them in specifying relative paths
239
+ $ # so, 'ls', 'ls .' and 'ls backups/..' will give same result
240
+ $ ls -aF backups/dot_files/
241
+ ./ ../ .bashrc .inputrc .vimrc
242
+
243
+ $ # use -A option to show hidden files excluding . and .. special directories
244
+ $ ls -A backups/dot_files/
245
+ .bashrc .inputrc .vimrc
246
+ ```
247
+
248
+ * use ` -R ` option to recursively list sub-directories too
249
+
250
+ ``` bash
251
+ $ ls -ARF
252
+ .:
253
+ backups/ hello_world.py* palindrome.py* projects@ todo
254
+ ch.sh* ip.txt power.log report.log workshop_brochures/
255
+
256
+ ./backups:
257
+ chrome_bookmarks_02_07_2018.html dot_files/
258
+
259
+ ./backups/dot_files:
260
+ .bashrc .inputrc .vimrc
261
+
262
+ ./workshop_brochures:
263
+ Python_workshop_2017.pdf Scripting_course_2016.pdf
264
+ ```
265
+
266
+ * often, we want to prune which files/folders are to be listed
267
+ * commands like ` find ` provide extensive features in this regard
268
+ * the shell itself provides a matching technique called glob/wildcards
269
+ * see [ Shell wildcards] ( ./Shell.md#wildcards ) section for more examples and details
270
+ * beginners incorrectly associate globbing with ` ls ` command, as a demonstration globbing results are shown using ` echo ` command first
271
+
272
+ ``` bash
273
+ $ # all unquoted arguments are subjected to shell globbing interpretation
274
+ $ echo * .py * .log
275
+ hello_world.py palindrome.py power.log report.log
276
+ $ echo ' *.py' * .log
277
+ * .py power.log report.log
278
+
279
+ $ # long list only files ending with .py
280
+ $ ls -l * .py
281
+ -rwxrwxr-x 1 learnbyexample eg 41 Aug 21 2017 hello_world.py
282
+ -rwxrwxr-x 1 learnbyexample eg 1236 Aug 21 2017 palindrome.py
283
+
284
+ $ # match all filenames starting with alphabets c/d/e/f/g/h/i
285
+ $ echo [c-i]*
286
+ ch.sh hello_world.py ip.txt
287
+ $ ls -sh [c-i]*
288
+ 4.0K ch.sh 4.0K hello_world.py 4.0K ip.txt
289
+ ```
290
+
291
+ * use ` -d ` option to not show directory contents
292
+
293
+ ``` bash
294
+ $ echo b*
295
+ backups
296
+ $ # since backups is a directory, ls will list its contents
297
+ $ ls b*
298
+ chrome_bookmarks_02_07_2018.html dot_files
299
+ $ # -d option will show the directory entry instead of its contents
300
+ $ ls -d b*
301
+ backups
302
+
303
+ $ # a simple way to get only the directory entries
304
+ $ # assuming simple filenames without spaces/newlines/etc
305
+ $ echo * /
306
+ backups/ projects/ workshop_brochures/
307
+ $ ls -d * /
308
+ backups/ projects/ workshop_brochures/
309
+ ```
310
+
311
+ ** Further Reading**
312
+
313
+ * ` man ls ` and ` info ls ` for more options and complete documentation
157
314
* [ ls Q&A on unix stackexchange] ( https://unix.stackexchange.com/questions/tagged/ls?sort=votes&pageSize=15 )
158
315
* [ ls Q&A on stackoverflow] ( https://stackoverflow.com/questions/tagged/ls?sort=votes&pageSize=15 )
159
- * [ avoid parsing output of ls] ( https://mywiki.wooledge.org/ParsingLs )
160
- * [ why not parse ls?] ( https://unix.stackexchange.com/questions/128985/why-not-parse-ls )
316
+ * [ mywiki.wooledge: avoid parsing output of ls] ( https://mywiki.wooledge.org/ParsingLs )
317
+ * [ unix.stackexchange: why not parse ls?] ( https://unix.stackexchange.com/questions/128985/why-not-parse-ls )
318
+ * [ unix.stackexchange: What are ./ and ../ directories?] ( https://unix.stackexchange.com/questions/63081/what-are-and-directories )
161
319
162
320
<br >
163
321
164
322
## <a name =" mkdir " ></a >mkdir
165
323
166
324
> make directories
167
325
326
+ File names can use any character other than ` / ` and ASCII NUL character
327
+
168
328
** Examples**
169
329
170
330
* ` mkdir project_adder ` create folder project_adder in current directory
0 commit comments