Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Rejected.
This suggested edit was rejected 6 months ago by alx‭:

pipes first feel more readable to me

62 / 255
  • 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>
  • 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>

Suggested 6 months ago by terdon‭