Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

1 votes
3 answers
2438 views
Searching /usr/dict/words to find words with certain properties
I would like to write a script to search through /usr/dict/words to find all words that meet some criteria I specify. For example, finding all palindromic words (like "racecar", "madam", etc.) or finding all words where the first and second halves reversed also form a word (like "german" and "manger...
I would like to write a script to search through /usr/dict/words to find all words that meet some criteria I specify. For example, finding all palindromic words (like "racecar", "madam", etc.) or finding all words where the first and second halves reversed also form a word (like "german" and "manger"). The framework of the script would be a simple loop to read each word in the dictionary, and I could change the criteria depending on what I want to look for by substituting an expression or something similar. I figure I would need to involve regular expressions somehow (or otherwise find a way to look at individual characters in each word). I would also need a way to compare the characters in my current word to the other words in the dictionary (such as with my second example above). What would be the best tool(s) to use for this task?
user161121
Mar 14, 2016, 11:31 PM • Last activity: Jul 23, 2025, 10:05 AM
3 votes
1 answers
2294 views
Wildcards in exclude-filelist for duplicity
I am trying to exclude a "bulk" folder in each home directory from the backup. For this purpose, I have a line - /data/home/*/bulk in my exclude-filelist file. However, this doesn't seem to be recognised: Warning: file specification '/data/home/*/bulk' in filelist exclude-list-test.txt doesn't start...
I am trying to exclude a "bulk" folder in each home directory from the backup. For this purpose, I have a line - /data/home/*/bulk in my exclude-filelist file. However, this doesn't seem to be recognised: Warning: file specification '/data/home/*/bulk' in filelist exclude-list-test.txt doesn't start with correct prefix /data/home/kay/bulk. Ignoring. Is there a way? BTW: is the format in general compatible with rsync's exclude-from? I have a working exclude list for that, where this wildcard expression works.
mcandril (273 rep)
Oct 6, 2014, 11:30 AM • Last activity: Jun 29, 2025, 09:00 PM
3 votes
3 answers
847 views
I need a standard way of expressing as a wildcard expression a set of subdirectories of a top level directory all with the same name
I don't know if such a standard exists, but I thought I'd ask the question. Is there a way to express the following as a wildcard expression: all subdirectories named 'process' residing under a top level directory at any level of nesting? so I want to include top/process top/sub1/process top/sub2/pr...
I don't know if such a standard exists, but I thought I'd ask the question. Is there a way to express the following as a wildcard expression: all subdirectories named 'process' residing under a top level directory at any level of nesting? so I want to include top/process top/sub1/process top/sub2/process top/sub1/sub2/sub98/process etc. I do not want any directories named process that are not nested under top. **UPDATE**: I should have explained my use case originally. It is not a **command** I issue from a shell or other way. It is a **list** I provide to an outside vendor - in this case IDrive - for their listing of files to exclude from backup. I don't know what syntax they use, and I had a devil of a time explaining it to their tech support, who were actually pretty good. Not a simple problem.
Steve Cohen (519 rep)
Jun 22, 2025, 10:03 PM • Last activity: Jun 28, 2025, 06:36 AM
42 votes
10 answers
57665 views
How do I create a directory in all subdirectories?
Suppose I have a directory structure like this: $ [~/practice] ls a/ b/ c/ d/ Now I want to create a directory `tmp1` in all sub directories of `practice` and I do this: $ [~/practice] mkdir */tmp1 mkdir: cannot create directory `*/tmp1': No such file or directory Then I try the `-p` switch and I en...
Suppose I have a directory structure like this:
$ [~/practice] ls
a/ b/ c/ d/
Now I want to create a directory tmp1 in all sub directories of practice and I do this:
$ [~/practice] mkdir */tmp1
mkdir: cannot create directory `*/tmp1': No such file or directory
Then I try the -p switch and I endup with a directory named * with a sub directory tmp1
$ [~/practice] mkdir -p */tmp1

$ [~/practice] ls
*/ a/ b/ c/ d/
I know the use of -p switch is to create multiple nonexistent directories. I just thought it might help. How do I create tmp1 in all subdirectories at once? If this can be done, how do I extend it to create \tmp1, \tmp2, \tmp3 in \a, \b and \c at once? **Edit:** I missed mentioning that the directories don't have to be simple and in order, like a, b, c etc., and the directory to be created is not necessarily like tmp1, tmp2.
$ [~/practice] ls
dog/ cat/ rat/
In them, I would like to have something like
$ [~/practice] ls *
dog:
red/ blue/

cat:
red/ blue/

rat:
red/ blue/
Animesh D (2017 rep)
Jan 20, 2013, 03:08 PM • Last activity: Jun 23, 2025, 02:35 PM
14 votes
5 answers
18308 views
Bash globbing that matches all files except those with a specific extension, that works on filenames that include dot characters
I'm doing some stuff with audio files, most but not all of which are mp3 files. Now I want to run some commands on only the files which are not mp3 files, or only those which don't have a `.mp3` extension. I consider myself pretty good at regular expressions, but not so much at file globbing, which...
I'm doing some stuff with audio files, most but not all of which are mp3 files. Now I want to run some commands on only the files which are not mp3 files, or only those which don't have a .mp3 extension. I consider myself pretty good at regular expressions, but not so much at file globbing, which is subtly different in unexpected ways. I looked around and learned from other SO & SE answers that Bash has "extended globbing" that allows me to do this: file ../foo/bar/*.!(mp3) But some of my filenames have dots in them besides the one forming the filename extension: ../foo/bar/Naked_Scientists_Show_19.10.15.mp3 ../foo/bar/YWCS_ep504-111519-pt1_5ej4_41cc9320.mp3_42827d48daefaa81ec09202e67fa8461_24419113.mp3 ../foo/bar/eLife_Podcast_19.09.26.mp3 ../foo/bar/gdn.sci.080428.bg.science_weekly.mp3 It seems the glob matches from the first dot onward, rather than from the last dot. I looked at the documentation but it seems they are far less powerful than regexes. But I didn't really grok everything as I don't spend that much time on *nix shells. Have I missed some way that I can still do this with Bash globbing? If not, a way to achieve the same thing with find or some other tool would still be worth knowing.
hippietrail (426 rep)
Dec 10, 2019, 03:03 PM • Last activity: Jun 21, 2025, 11:33 AM
3 votes
4 answers
5101 views
Use extended globbing to list files but not directories in bash
Let' say I have the following directories tree: . ├── 11 ├── 111 ├── 112 ├── 1121 ├── 113 ├── 11a ├── 11a1 ├── 1a1 ├── 1a2 ├── 1aa1.png ├── 2a1 ├── a. ├── a1a ├── a1a.jpg ├── a2a ├── aa ├── -aa ├── aa.gif ├── aa.jpg ├── aa.png ├── aa.tiff ├── a.exe ├── a.gif ├── a.html ├── a.jpg ├── a.png ├── a.tiff...
Let' say I have the following directories tree: . ├── 11 ├── 111 ├── 112 ├── 1121 ├── 113 ├── 11a ├── 11a1 ├── 1a1 ├── 1a2 ├── 1aa1.png ├── 2a1 ├── a. ├── a1a ├── a1a.jpg ├── a2a ├── aa ├── -aa ├── aa.gif ├── aa.jpg ├── aa.png ├── aa.tiff ├── a.exe ├── a.gif ├── a.html ├── a.jpg ├── a.png ├── a.tiff ├── b2 ├── b2a ├── ba.gif ├── ba.jpg ├── ba.png ├── ba.tiff ├── b.html ├── cb1.png ├── d.gif └── sub1 ├── d.gif └── sub2 So, if I want to match everything in . ($PWD) but files with ".jpg" extension, I do the following: ls !(*.jpg) But it outputs: 11 1121 11a1 1aa1.png a1a aa.gif a.exe a.png b2a ba.tiff d.gif 111 113 1a1 2a1 a2a aa.png a.gif a.tiff ba.gif b.html .gif 112 11a 1a2 a. aa aa.tiff a.html b2 ba.png cb1.png sub1: . .. d.gif sub2 I'd like to exclude both sub1 and all of its contents (even directories), ie: > sub1: > . .. d.gif sub2 I can exclude using: GLOBIGNORE='sub1' before of ls, but what happens if I have more directories with differents names? Is there a way to do that?
sebelk (4669 rep)
Jun 26, 2017, 06:04 PM • Last activity: May 31, 2025, 01:07 PM
2 votes
2 answers
135 views
How does `*\ *` work in bash?
There is [an answer from SuperUser][1], which renames filenames containing whitespace: for f in *\ *; do mv "$f" "${f// /_}"; done The part I don't understand is `*\ *`. The author wrote that: > `*\ *` selects all files with a space in their name as input for the the for loop. The pattern `*X*` sele...
There is an answer from SuperUser , which renames filenames containing whitespace: for f in *\ *; do mv "$f" "${f// /_}"; done The part I don't understand is *\ *. The author wrote that: > *\ * selects all files with a space in their name as input for the the for loop. The pattern *X* selects all files with X in their name, and for the special character space, we have to escape it with a slash so that bash doesn't treat it as separating different arguments. Since * does not match a space, why does *\ * also match files with multiple space character when it only has one space in it?
glacier (391 rep)
Apr 22, 2025, 01:46 PM • Last activity: May 19, 2025, 04:45 PM
2 votes
2 answers
243 views
How to remove similar folder from multiple locations in terminal
I have a folder 'reg_standard' nested within 68 different folders that I want to remove. The path to this folder looks something like: /Volumes/.../sub-???/run_?.feat/reg_standard where `'?'` is a stand-in for all the different folders that start with 'sub' or 'run'. For example, I have sub-101, sub...
I have a folder 'reg_standard' nested within 68 different folders that I want to remove. The path to this folder looks something like: /Volumes/.../sub-???/run_?.feat/reg_standard where '?' is a stand-in for all the different folders that start with 'sub' or 'run'. For example, I have sub-101, sub-102, sub-162 (34 in total) and run_1 and run_2 folders nested within each sub-???, making a total of 68 folders in total where reg_standard exists. How can I remove this folder and all its contents efficiently? Do I use rm -r? or find ... - delete? I use MacOS. Thank you!
user556056 (23 rep)
Jan 9, 2023, 06:53 PM • Last activity: May 17, 2025, 09:07 AM
2 votes
1 answers
6946 views
Glob and regex matching
`Bash` has two types of pattern matching, `Glob` and `Regex`. The general rule of usage seems to be that 1) the simpler `glob` is done to search filenames 2) `regex` is used for searching text. `Glob` uses the metacharacters at the front, `regex` uses the metacharacters at the end of the `pattern`....
Bash has two types of pattern matching, Glob and Regex. The general rule of usage seems to be that 1) the simpler glob is done to search filenames 2) regex is used for searching text. Glob uses the metacharacters at the front, regex uses the metacharacters at the end of the pattern. Glob Regex ?(pattern) (pattern)? *(pattern) (pattern)* +(pattern) (pattern)+ I therefore have difficulty understanding how file matching with wildcards (e.g. *.sh). Are wildcards something different than glob patterns? From what I see, the search pattern *.sh does not include a metacharacter to match any character after *.
Vera (1363 rep)
Jan 29, 2023, 08:55 PM • Last activity: May 15, 2025, 12:39 PM
0 votes
0 answers
29 views
Wildcard not expanding. sudo to blame?
RHEL 8. $ alias suip alias suip='sudo -iu postgres' If I specify the complete filename, the `ls` command works as expected: suip ls ~postgres/logs/pgbackrest_2025-04-30_01:00:01_incr.log /var/lib/pgsql/logs/pgbackrest_2025-04-30_01:00:01_incr.log Not so much, though, when I use a wildcard: $ suip ls...
RHEL 8. $ alias suip alias suip='sudo -iu postgres' If I specify the complete filename, the ls command works as expected: suip ls ~postgres/logs/pgbackrest_2025-04-30_01:00:01_incr.log /var/lib/pgsql/logs/pgbackrest_2025-04-30_01:00:01_incr.log Not so much, though, when I use a wildcard: $ suip ls ~postgres/logs/pgbackrest_2025-04-30*log ls: cannot access '/var/lib/pgsql/logs/pgbackrest_2025-04-30*log': No such file or directory The ultimate goal is to find the newest file matching pgbackrest_*log. Using find(1) does the job, but I'm wondering why ls(1) *does not* expand the wildcard. $ suip find ~postgres/logs -name 'pgbackrest_*log' | sort | tail -n1 /var/lib/pgsql/logs/pgbackrest_2025-05-01_01:00:01_incr.log
RonJohn (1421 rep)
May 1, 2025, 02:31 PM
68 votes
6 answers
49629 views
The best way to expand glob pattern?
I need to expand a glob pattern (like `../smth*/*`, or `/etc/cron*/`) into a list of files, programmatically. What would be the best way to do it?
I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?
Rogach (6533 rep)
Mar 12, 2012, 01:28 PM • Last activity: Mar 24, 2025, 04:21 AM
1 votes
3 answers
223 views
Fish: To match only files or only folders
In Zsh, to match only non-directory files or only regular files or only directory files, I can use `*(^/)`, `*(.)` and `*(/)` (or `*(^/D)`, `*(.D)` and `*(/D)`, if I need to include hidden ones). Is it possible to match only files or only folders in Fish?
In Zsh, to match only non-directory files or only regular files or only directory files, I can use *(^/), *(.) and *(/) (or *(^/D), *(.D) and *(/D), if I need to include hidden ones). Is it possible to match only files or only folders in Fish?
jsx97 (1347 rep)
Jul 28, 2024, 11:55 AM • Last activity: Mar 14, 2025, 05:19 PM
9 votes
1 answers
658 views
Why does bash give the following result after brace expansion?
I am using linux and the following version of the bash: ``` GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) ``` When I type: ``` echo file{[1,2],3}.txt ``` I expect brace expansion to be done first, so: ``` echo file[1,2].txt file3.txt ``` After that, I expect it to stop there, since [1,2]...
I am using linux and the following version of the bash:
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
When I type:
echo file{[1,2],3}.txt
I expect brace expansion to be done first, so:
echo file[1,2].txt file3.txt
After that, I expect it to stop there, since [1,2] is not a valid globbing. However, it gives the following as output:
file[1.txt file2].txt file3.txt
Why? List of files in current directory (result of ls command):
go.mod  hello.sh  hi  main.go
Yakog (517 rep)
Feb 22, 2025, 01:46 PM • Last activity: Feb 24, 2025, 12:24 PM
4 votes
3 answers
1243 views
Command line glob to view multiple images using ImageMagick's display
How can I open multiple images files using ImageMagick's `display` command? If I enter say `display *.png` in a directory containing multiple PNG files, only one is displayed. I think I'd prefer them to appear one at a time, but all at once could be useful too.
How can I open multiple images files using ImageMagick's display command? If I enter say display *.png in a directory containing multiple PNG files, only one is displayed. I think I'd prefer them to appear one at a time, but all at once could be useful too.
user7543 (274 rep)
Sep 30, 2023, 10:01 PM • Last activity: Jan 28, 2025, 07:09 AM
4 votes
1 answers
134 views
Bash extglob with ignored pattern
Suppose I have these files: ``` foo/bar/baz/test.js foo/bar/baz/test.min.js ``` If I run: ```sh shopt -s globstar shopt -s extglob echo foo/bar/**/*!(.min).js ``` ...that will nonetheless match the `test.min.js` file. How do I ignore it?
Suppose I have these files:
foo/bar/baz/test.js
foo/bar/baz/test.min.js
If I run:
shopt -s globstar
shopt -s extglob
echo foo/bar/**/*!(.min).js
...that will nonetheless match the test.min.js file. How do I ignore it?
lonix (1965 rep)
Dec 13, 2024, 11:01 AM • Last activity: Jan 6, 2025, 01:58 PM
5 votes
4 answers
1318 views
I want to copy files that do not have a number in their name
[![These are all the files][1]][1] [1]: https://i.sstatic.net/Z4sKB.png I want to use cp command using file globing to copy all those files that do not have a numeric value. The copied files should not include 12a, ghf3 and s2d. I tried using `cp [!0-9] *[!0-9] *[!0-9] destination/`
These are all the files I want to use cp command using file globing to copy all those files that do not have a numeric value. The copied files should not include 12a, ghf3 and s2d. I tried using cp [!0-9] *[!0-9] *[!0-9] destination/
Raghav Arora (51 rep)
Jun 16, 2023, 08:40 AM • Last activity: Jan 5, 2025, 11:56 AM
0 votes
2 answers
136 views
variable assignment doesn't create one same object at least for grep
The problem is as follows (Here I don't use [`find`][1] since it doesn't support double-asterisk wildcard `**`): ```bash $ FILES=(foo/**/*.suffix bar/**/*.suffix2) $ grep baz "${FILES[@]}" # works # I use this to create one local var with local in one function $ SUBFILES="${FILES[@]}" $ grep baz "${...
The problem is as follows (Here I don't use find since it doesn't support double-asterisk wildcard **):
$ FILES=(foo/**/*.suffix bar/**/*.suffix2)
$ grep baz "${FILES[@]}" # works
# I use this to create one local var with local in one function
$ SUBFILES="${FILES[@]}"
$ grep baz "${SUBFILES[@]}" # doesn't work
I used od to check them but they are same at least have the same number of "\n" and the same length (I didn't check character by character. But at a glance they are same).
$ echo ${SUBFILES[@]} | od -c
$ echo ${FILES[@]} | od -c
Why does the variable assignment not create one object still work for grep?
An5Drama (173 rep)
Jan 2, 2025, 08:43 AM • Last activity: Jan 2, 2025, 06:52 PM
0 votes
2 answers
2666 views
Bash script to recursively obtain file and directory permissions, compare them to desired permissions and return if correct or not
I am trying to write a script which would retrieve the permissions of a group of files and directories. Then check each permission to see if they are set correctly. If the permissions are not set correctly then I would like it to echo which directory or group of files permissions were incorrectly se...
I am trying to write a script which would retrieve the permissions of a group of files and directories. Then check each permission to see if they are set correctly. If the permissions are not set correctly then I would like it to echo which directory or group of files permissions were incorrectly set. I have used find to recursively find all files and directories inside a certain directory and then execute stat to return the current permissions. From the returned list of permissions I then use an if-then statement to check if any files or directories have unexpected permissions. This is accomplished by the != operator and using pattern matching. So all files should have permissions set to 444 and directories to 555 and if not return that the permissions are wrong. for site in $(echo /var/www/*) do permcheckfile=$(find $site -type f -exec stat -c '%a' '{}' +) permcheckdir=$(find $site -type d -exec stat -c '%a' '{}' +) if [[ $permcheckfile != *444 ]] then echo "$site file permissions are wrong" else echo "$site file permissions are correct" fi if [[ $permcheckdir != *555 ]] then echo "$site directory permissions are wrong" else echo "$site directory permissions are correct" fi done The problem found with the script above is sometimes it will return false positives. Where I am going wrong? Is there a better way?
SamuelR (3 rep)
Feb 14, 2017, 08:05 PM • Last activity: Dec 31, 2024, 12:34 PM
18 votes
1 answers
16577 views
zsh: is there a problem with always enabling extended glob?
I recetnly came across setopt extended_glob ...in order to enable extended globbing which allows for a number of cool wildcard additions, like excluding specific patterns, for example: ls ^foo* ...will use `ls` on every path in your current directory except for patterns that match `foo*`. I found on...
I recetnly came across setopt extended_glob ...in order to enable extended globbing which allows for a number of cool wildcard additions, like excluding specific patterns, for example: ls ^foo* ...will use ls on every path in your current directory except for patterns that match foo*. I found one tutorial suggesting to put setopt extended_glob inside your .zshrc, but I guess since many zsh config templates miss that option and the option being *disabled by default* it has some downsides or even side-effects? Or is it absolutely harmless always enabling extended_glob via putting it inside one's .zshrc?
daniel451 (1107 rep)
Mar 17, 2018, 05:40 PM • Last activity: Dec 12, 2024, 07:02 AM
0 votes
4 answers
525 views
Excluding symbolic links from dir
I want to exclude symbolic links from a directory listing. I have been using the following command: dir --ignore=@ -F But the symbolic links are still being listed as showed. ![screenshot](https://i.sstatic.net/kWxi4.png) How can I exclude them when using the dir command? Thank you.
I want to exclude symbolic links from a directory listing. I have been using the following command: dir --ignore=@ -F But the symbolic links are still being listed as showed. ![screenshot](https://i.sstatic.net/kWxi4.png) How can I exclude them when using the dir command? Thank you.
Thanh (1 rep)
Oct 29, 2022, 01:26 AM • Last activity: Dec 6, 2024, 06:43 PM
Showing page 1 of 20 total questions