Post History
In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use while read: find . -type d \ | while read d; do find $d -type f -maxdepth 1 \ | hea...
Answer
#6: Post edited
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find . -type d \
| while read -r d; do- find $d -type f -maxdepth 1 \
- | head -n3;
- done;
- ```
(The above shows the first 3 files in each directory.)In general, you want to use `while read -r`, but if you know the path names are "safe", you can omit the `-r`.- However, this forces an invocation of the command or pipeline for each path name. In general, xargs(1) is the simplest (and usually fastest) way:
- ```sh
- find . -type f \
- | xargs mv -t /tmp;
- ```
(The above moves all files in `.` to `/tmp`.)
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find . -type d \
- | while read d; do
- find $d -type f -maxdepth 1 \
- | head -n3;
- done;
- ```
- Hardened version:
- ```sh
- find . -type d -print0 \
- | while IFS= read -r -d '' d; do
- find "$d" -type f -maxdepth 1 -print0 \
- | head -z -n3;
- done;
- ```
- Portable filenames should only use characters from the portable filename character set <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282>. But if you need to run your script in filesystems that may be compromised, or use non-portable file names for other reasons, use the hardened version.
- Read the corresponding documentation of the features:
- - find(1):
- - `-print0` <https://www.man7.org/linux/man-pages/man1/find.1.html#EXPRESSION>
- - sh(1):
- - `IFS=` <https://www.man7.org/linux/man-pages/man1/sh.1p.html#RATIONALE>
- - read(1):
- - `-r` <https://www.man7.org/linux/man-pages/man1/read.1p.html#OPTIONS>
- - `-d ''` <https://stackoverflow.com/questions/9612090/how-to-loop-through-file-names-returned-by-find#comment98776168_9612232>
- - head(1):
- - `-z` <https://www.man7.org/linux/man-pages/man1/head.1.html#DESCRIPTION>
- (The above shows the first 3 files in each directory.)
- However, this forces an invocation of the command or pipeline for each path name. In general, xargs(1) is the simplest (and usually fastest) way:
- ```sh
- find . -type f \
- | xargs mv -t /tmp;
- ```
- Hardened version:
- ```sh
- find . -type f -print0 \
- | xargs -0 mv -t /tmp;
- ```
- (The above moves all files in `.` to `/tmp`.)
- - xargs(1):
- - `-0` <https://www.man7.org/linux/man-pages/man1/xargs.1.html#OPTIONS>
#5: Post edited
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find . -type d \
- | while read -r d; do
- find $d -type f -maxdepth 1 \
- | head -n3;
- done;
- ```
- (The above shows the first 3 files in each directory.)
- In general, you want to use `while read -r`, but if you know the path names are "safe", you can omit the `-r`.
However, this forces an invocation of the command for each path name. In general, xargs(1) is the simplest (and usually fastest) way:- ```sh
- find . -type f \
- | xargs mv -t /tmp;
- ```
- (The above moves all files in `.` to `/tmp`.)
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find . -type d \
- | while read -r d; do
- find $d -type f -maxdepth 1 \
- | head -n3;
- done;
- ```
- (The above shows the first 3 files in each directory.)
- In general, you want to use `while read -r`, but if you know the path names are "safe", you can omit the `-r`.
- However, this forces an invocation of the command or pipeline for each path name. In general, xargs(1) is the simplest (and usually fastest) way:
- ```sh
- find . -type f \
- | xargs mv -t /tmp;
- ```
- (The above moves all files in `.` to `/tmp`.)
#4: Post edited
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find . -type d \
- | while read -r d; do
- find $d -type f -maxdepth 1 \
- | head -n3;
- done;
- ```
- (The above shows the first 3 files in each directory.)
In general, you want to use `while read -r`, but if you know the filenames are "safe", you can omit the `-r`.However, this forces an invocation of the command for each file name. In general, xargs(1) is the simplest (and usually fastest) way:- ```sh
- find . -type f \
- | xargs mv -t /tmp;
- ```
- (The above moves all files in `.` to `/tmp`.)
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find . -type d \
- | while read -r d; do
- find $d -type f -maxdepth 1 \
- | head -n3;
- done;
- ```
- (The above shows the first 3 files in each directory.)
- In general, you want to use `while read -r`, but if you know the path names are "safe", you can omit the `-r`.
- However, this forces an invocation of the command for each path name. In general, xargs(1) is the simplest (and usually fastest) way:
- ```sh
- find . -type f \
- | xargs mv -t /tmp;
- ```
- (The above moves all files in `.` to `/tmp`.)
#3: Post edited
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
find \| while read -r f; domv $f $f.bak;- done;
- ```
In general, you want to use `while read -r`, but if you know the filenames are "safe", you can omit the `-r`.
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find . -type d \
- | while read -r d; do
- find $d -type f -maxdepth 1 \
- | head -n3;
- done;
- ```
- (The above shows the first 3 files in each directory.)
- In general, you want to use `while read -r`, but if you know the filenames are "safe", you can omit the `-r`.
- However, this forces an invocation of the command for each file name. In general, xargs(1) is the simplest (and usually fastest) way:
- ```sh
- find . -type f \
- | xargs mv -t /tmp;
- ```
- (The above moves all files in `.` to `/tmp`.)
#2: Post edited
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find \
| while read f; do- mv $f $f.bak;
- done;
```
- In some cases, when you want to apply a pipeline or a complex command to each file, I find it useful to use `while read`:
- ```sh
- find \
- | while read -r f; do
- mv $f $f.bak;
- done;
- ```
- In general, you want to use `while read -r`, but if you know the filenames are "safe", you can omit the `-r`.