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
2 answers
132 views
Line editor ed saves some filenames in single quotes followed by one space
I'm following *The Unix Programming Environment* exercises and have encountered some weird issues. While going through the `ed` exercise, when you just start `ed`, type in the exercise, then do w poem It saves in the filesystem as `'poem '` I tested another file, start `ed`, type in some words, w ra...
I'm following *The Unix Programming Environment* exercises and have encountered some weird issues. While going through the ed exercise, when you just start ed, type in the exercise, then do w poem It saves in the filesystem as 'poem ' I tested another file, start ed, type in some words, w raistlin and it saves as 'raistlin ' with the same result (filename in single quotes followed by one space), though I tested on a third, and if I start off with the file name it saves as a normal file. Has anyone else seen these results? I know it's a little esoteric, I think I'm probably 1 of 7 people on the whole earth using ed this weekend. I'm using Manjaro Arch.
Mark Scheck (280 rep)
Nov 28, 2020, 08:06 AM • Last activity: Jul 29, 2025, 08:56 AM
6 votes
8 answers
14700 views
Check if shell variable contains an absolute path
I want to check if a shell variable contains an absolute path. I don't care if the path exists or not—if it doesn't I'm going to create it—but I do want to ensure that I'm dealing with an absolute pathname. My code looks something like the following: myfunction() { [ magic test to see if "$1" is an...
I want to check if a shell variable contains an absolute path. I don't care if the path exists or not—if it doesn't I'm going to create it—but I do want to ensure that I'm dealing with an absolute pathname. My code looks something like the following: myfunction() { [ magic test to see if "$1" is an absolute path ] || return 1 mkdir -p "$(dirname "$1")" || return 1 commands >> "$1" } Or, the use case where the absolute path to be verified is intended to be a directory: anotherfunction() { [ same magic test ] || return 1 mkdir -p "$1" dostuff >> "$1/somefile" } If this were awk I would do the check like so: myvar ~ /^\// There *must* be a clean way to do this with the shell's string handling, but I'm having trouble coming up with it. (Mentioning a bash-specific solution would be fine but I'd like to know how to do this portably, also. POSIX string handling seems like it should be sufficient for this.)
Wildcard (37446 rep)
Jan 20, 2016, 12:57 AM • Last activity: Jul 20, 2025, 07:52 AM
0 votes
1 answers
43 views
Alias for rm does not include file name with @$
I am trying to create an alias for the rm command in the /root/.bashrc file on a VirtualBox Redhat VM (running RHEL 9). I cannot get it to work properly. This is an excerpt of my .bashrc file: ``` alias rm='printf "rm: cannot remove %s: Permission denied\n" "$@" && w >> /tmp/logfile_20090204_001.log...
I am trying to create an alias for the rm command in the /root/.bashrc file on a VirtualBox Redhat VM (running RHEL 9). I cannot get it to work properly. This is an excerpt of my .bashrc file:
alias rm='printf "rm: cannot remove %s: Permission denied\n" "$@" &&  w >> /tmp/logfile_20090204_001.log'
sudo() {
	if [[ "$1" == "rm" ]]; then
		for file in "${@:2}"; do
			printf "rm: cannot remove %s: Permission denied\n" "$file"
		done
		w >> /tmp/logfile_20190204_002.log
	else
		command sudo "$@"
	fi
}
I reload it with . /root/.bashrc. The result I get when using rm is
: cannot remove : Permission denied
(no file name). I have tried replacing printf with echo, switching the single and double quotes and modifying the bash_aliases file instead of the .bashrc file, and nothing seems to work. How can I get this alias to print my file name (with and without sudo)? Thank you in advance for your help!
minionsaregreat (3 rep)
Jul 10, 2025, 12:42 PM • Last activity: Jul 10, 2025, 01:02 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
-4 votes
1 answers
85 views
How combination of find with sh solves problem with filenames
I was directed to this StackExchange post: [Why is looping over find's output bad practice?][1] [1]: https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice The core issue is that Unix filenames can contain any character except the null byte (\0). This means the...
I was directed to this StackExchange post: Why is looping over find's output bad practice? The core issue is that Unix filenames can contain any character except the null byte (\0). This means there is no printable character you can reliably use as a delimiter when processing filenames - newlines, spaces, and tabs can all appear in valid filenames. A common solution is to use a null byte as the delimiter, which is supported by GNU find with the -print0 option. This allows you to safely process filenames using tools like xargs -0 or while read -d ''. However, not all versions of find (especially non-GNU variants) support -print0. This raises the question: What should you do for portability when -print0 isn't available? Frankly, it seems that find implementations lacking -print0 are fundamentally flawed for robust scripting and should not be relied upon in scripts that need to handle arbitrary filenames safely. There was the suggestion to use find in combination with sh find dirname ... -exec sh -c 'for f do somecommandwith "$f"; done' find-sh {} + How does this fix the problem? One is still using a defective find. The link did not provide a clear explanation of why the combination of find with sh should work. How does it solve the problem?
Filangieri (179 rep)
Jun 9, 2025, 05:15 PM • Last activity: Jun 9, 2025, 11:33 PM
1 votes
2 answers
3966 views
Characters best avoided in filenames when used in Bash, e.g. `?`
1. For example: $ ls -l total 344 -r-------- 1 t t 145657 Mar 11 01:53 joeltest-slides.pdf -rw-rw-r-- 1 t t 166814 Mar 11 01:55 The Joel Test: 12 Steps to Better Code? by Joel Spolsky.pdf drwx-w--w- 2 t t 4096 Sep 19 2012 The Joel Test 12 Steps to Better Code_files -rw--w--w- 1 t t 31940 Feb 12 2011...
1. For example: $ ls -l total 344 -r-------- 1 t t 145657 Mar 11 01:53 joeltest-slides.pdf -rw-rw-r-- 1 t t 166814 Mar 11 01:55 The Joel Test: 12 Steps to Better Code? by Joel Spolsky.pdf drwx-w--w- 2 t t 4096 Sep 19 2012 The Joel Test 12 Steps to Better Code_files -rw--w--w- 1 t t 31940 Feb 12 2011 The Joel Test 12 Steps to Better Code.html $ mv The\ Joel\ Test\:\ 12\ Steps\ to\ Better\ Code{ \ by\ Joel\ Spolsky.pdf,.pdf} mv: missing destination file operand after ‘The Joel Test: 12 Steps to Better Code{’ Try 'mv --help' for more information. What does missing destination file operand mean? Is it because ? in the filename? 1. In Bash, as far as performing file operations by builtin or external commands is concerned, what characters are best avoided when naming files? For exmple, Does the above example imply ? is one of them? Does [my previous post](https://unix.stackexchange.com/questions/268593/why-can-file-expansion-work-for-filenames-with-a-newline-character) imply the new line character is one of them? Does [my previous](https://unix.stackexchange.com/questions/266217/find-and-rename-files-including-directories-whose-filenames-contain-space) [posts](https://unix.stackexchange.com/questions/266216/search-for-and-remove-files-safely) imply the white space is one of them? 3. Is it correct that, from Linux's point of view, there is no restriction on characters that can be used in filenames? Neither from filesystem type (ext4) 's point of view?
Tim (106422 rep)
Mar 11, 2016, 07:09 AM • Last activity: Jun 9, 2025, 07:40 PM
0 votes
2 answers
91 views
Safely Handling Filenames with Newlines Using find and while read
I want to handle filenames with newlines in a bash script rather than having find "$search_dir" -type f | while IFS= read -r file; do Perhaps something like the following would do it find "$search_dir" -type f -print0 | while read -d '' -r file; do Would I also need to include `IFS=` ?
I want to handle filenames with newlines in a bash script rather than having find "$search_dir" -type f | while IFS= read -r file; do Perhaps something like the following would do it find "$search_dir" -type f -print0 | while read -d '' -r file; do Would I also need to include IFS= ?
Filangieri (179 rep)
Jun 8, 2025, 10:26 PM • Last activity: Jun 9, 2025, 03:21 PM
13 votes
5 answers
12974 views
display filename followed by content without interaction
My program writes it state to few files every 30 seconds or so (overwrites) and I would like to look at them with the filenames displayed like "more" does. Like: $ echo 1234 > test00 $ echo 5678 > test01 $ ls test00 test01 $ more * :::::::::::::: test00 :::::::::::::: 1234 :::::::::::::: test01 ::::...
My program writes it state to few files every 30 seconds or so (overwrites) and I would like to look at them with the filenames displayed like "more" does. Like: $ echo 1234 > test00 $ echo 5678 > test01 $ ls test00 test01 $ more * :::::::::::::: test00 :::::::::::::: 1234 :::::::::::::: test01 :::::::::::::: 5678 For "more" you have to press space to get next file. * What I want is the filenames before content in some way * I tried "tail", but have to Ctrl+C, Can write a small program but I’m wondering if any standard unix program can do this ( display name of file, followed by content of file ) without further user interaction as with "more"
AlfredoG (133 rep)
Feb 16, 2015, 06:43 AM • Last activity: May 28, 2025, 03:19 PM
4 votes
1 answers
694 views
How can I remove special non-characters that are not part of national alphabets like faces from filenames, keeping any national alphabets intact?
How can I remove special non-characters e.g. faces, avatars, emojis, symbols that are not part of national alphabets from filenames while keeping any national alphabets intact? e.g. `myfilename_hhjs_φφφδσε2_五(wǔ)__😎💃🏻` I want this result: e.g. `myfilename_hhjs_φφφδσε2_五(wǔ)__...
How can I remove special non-characters e.g. faces, avatars, emojis, symbols that are not part of national alphabets from filenames while keeping any national alphabets intact? e.g. myfilename_hhjs_φφφδσε2_五(wǔ)__😎💃🏻 I want this result: e.g. myfilename_hhjs_φφφδσε2_五(wǔ)__ The faces may are vary as well their position. I want Greek or Chinese or whatever national alphabet characters to remain intact.
Estatistics (350 rep)
May 7, 2025, 08:38 AM • Last activity: May 7, 2025, 11:01 PM
12 votes
7 answers
3372 views
Idiomatic way of generating a unique filename?
In a script I'm writing, I want to create something temporary on my filesystem, but - it's not in `/tmp` but elsewhere, and may not be a file nor a directory (e.g. maybe it's a named pipe or a symbolic link). The point is - I'll have do the creation myself. Now, I want to use a unique filename for m...
In a script I'm writing, I want to create something temporary on my filesystem, but - it's not in /tmp but elsewhere, and may not be a file nor a directory (e.g. maybe it's a named pipe or a symbolic link). The point is - I'll have do the creation myself. Now, I want to use a unique filename for my temporary, so that future invocations of the utility plus any other running code won't try to use the same name. If I were just creating a temporary file or directory in /tmp, I could use mktemp. But - what do I do when I only want to generate the name?
einpoklum (10753 rep)
Apr 27, 2025, 09:08 AM • Last activity: Apr 30, 2025, 08:14 AM
220 votes
8 answers
44345 views
Why is looping over find's output bad practice?
This question is inspired by [_Why is using a shell loop to process text considered bad practice ?_][1] I see these constructs for file in `find . -type f -name ...`; do smth with ${file}; done and for dir in $(find . -type d -name ...); do smth with ${dir}; done being used here almost on a daily ba...
This question is inspired by _Why is using a shell loop to process text considered bad practice ?_ I see these constructs for file in find . -type f -name ...; do smth with ${file}; done and for dir in $(find . -type d -name ...); do smth with ${dir}; done being used here almost on a daily basis even if some people take the time to comment on those posts explaining why this kind of stuff should be avoided... Seeing the number of such posts (and the fact that sometimes those comments are simply ignored) I thought I might as well ask a question: *Why is looping over find's output bad practice and what's the proper way to run one or more commands for each file name/path returned by find ?*
don_crissti (85483 rep)
Nov 7, 2016, 06:22 PM • Last activity: Apr 19, 2025, 09:44 AM
8 votes
1 answers
3358 views
Why is mktemp -u considered "unsafe"?
I read the `--help` text for `mktemp` recently (the `man` page wasn't available) and came upon this: -u, --dry-run do not create anything; merely print a name (unsafe) Why is this "unsafe"? Is there any specific reason for this being marked as such?
I read the --help text for mktemp recently (the man page wasn't available) and came upon this: -u, --dry-run do not create anything; merely print a name (unsafe) Why is this "unsafe"? Is there any specific reason for this being marked as such?
S.S. Anne (552 rep)
Sep 8, 2019, 07:50 PM • Last activity: Mar 25, 2025, 02:29 PM
24 votes
3 answers
62304 views
What are ./ and ../ directories?
Simple question, but I'm not sure where to look and google doesn't respond to periods and slashes. I'm just trying to count the # of files & directories in the current directory (not including subfolders/files) and was trying to differentiate `ls -1 | wc -l` and `ls | wc -l` since they seem identica...
Simple question, but I'm not sure where to look and google doesn't respond to periods and slashes. I'm just trying to count the # of files & directories in the current directory (not including subfolders/files) and was trying to differentiate ls -1 | wc -l and ls | wc -l since they seem identical. A site I was looking at said **"Keep in mind that this is also counting the ./ and ../ directories."** regarding the one with ls -1, and I'm not sure if that means it includes the directories before or something (which I don't want), but it didn't seem to do that from testing. Could someone confirm which one of those would be most adequate for counting # of files & directories in the current directory only (not sub) and what they mean by ./ and ../ directories?
Adam (291 rep)
Jan 30, 2013, 12:53 PM • Last activity: Mar 22, 2025, 02:38 PM
3 votes
3 answers
1062 views
Use of special characters in filename
I need to create a file named `*'test'*` If I write `touch \'test\'` `ls` shows `'test'` But if I write `touch \*\'test\'\*` `ls` now shows `*'\''test'\''*` What's the trick ? Can someone explain me what I did wrong ? My OS is Ubuntu 20.04
I need to create a file named *'test'* If I write touch \'test\' ls shows 'test' But if I write touch \*\'test\'\* ls now shows *'\''test'\''* What's the trick ? Can someone explain me what I did wrong ? My OS is Ubuntu 20.04
Shoopi (41 rep)
Feb 14, 2024, 06:08 PM • Last activity: Mar 17, 2025, 08:59 AM
6 votes
1 answers
333 views
Revert filenames after they were garbled by using different encoding
I have a file ``` СМП бваг™вга† ``` The first three letters are proper Cyrllic and the remaining part is [*mojibake*](https://en.wikipedia.org/wiki/Mojibake). > "_Mojibake_ is the garbled or gibberish text that is the result of text being decoded using an unintended character encoding." — Wikipedia...
I have a file
СМП бваг™вга†
The first three letters are proper Cyrllic and the remaining part is [*mojibake*](https://en.wikipedia.org/wiki/Mojibake) . > "_Mojibake_ is the garbled or gibberish text that is the result of text being decoded using an unintended character encoding." — Wikipedia Originally, it was
СМП структура
but then garbled somehow, most probably because of the file has been zip-ped on Windows XP and then un-zipped on a Mac, by an unskillful user. I tried to fix it using convmv and iconv, like this:
convmv -r -f cp1251 -t utf-8 DIR
ls | iconv -f cp1251 -t cp850 | iconv -f cp866
but didn't succeed yet. Could someone help with this? **update 1** Hexdump of СМП бваг™вга†:
0000000    d0  a1  d0  9c  d0  9f  20  d0  b1  d0  b2  d0  b0  d0  b3  e2
           С   **  М   **  П   **      б   **  в  **   а  **   г   **  ™
0000020    84  a2  d0  b2  d0  b3  d0  b0  e2  80  a0  0a                
           **  **  в  **   г  **   а   **  †   **  **  \n                
0000034
Hexdump of СМП структура:
0000000    d0  a1  d0  9c  d0  9f  20  d1  81  d1  82  d1  80  d1  83  d0
           С   **  М   **  П   **      с  **   т  **   р  **   у  **   к
0000020    ba  d1  82  d1  83  d1  80  d0  b0  0a                        
           **  т   **  у   **  р   **  а   **  \n                        
0000032
jsx97 (1347 rep)
Mar 14, 2025, 03:58 PM • Last activity: Mar 16, 2025, 06:00 PM
1 votes
3 answers
72 views
Duplicate and rename files with matching names but different extensions in bash?
Is there some *simple* way to duplicate both `.cpp` and `.h` files (or any multitude of extensions) simultaneously in `bash`, instead of: ```sh cp foo.cpp bar.cpp cp foo.h bar.h ``` I'm not looking for shell script answers or complicated `find` syntax or anything like that; I'm just wondering if the...
Is there some *simple* way to duplicate both .cpp and .h files (or any multitude of extensions) simultaneously in bash, instead of:
cp foo.cpp bar.cpp
cp foo.h bar.h
I'm not looking for shell script answers or complicated find syntax or anything like that; I'm just wondering if there's something maybe built into bash that does this in a simple way.
BrandonL (180 rep)
Feb 27, 2025, 05:46 PM • Last activity: Mar 8, 2025, 08:53 AM
229 votes
11 answers
185982 views
List files sorted numerically
I have a bunch of files from `log1` to `log164`. I'm trying to LIST the directory (sorted) in a UNIX terminal but the sort functions are only providing the format like this: home:logs Home$ ls -1 | sort log1.gz log10.gz log100.gz log101.gz log102.gz log103.gz log104.gz log105.gz log106.gz ...etc Wha...
I have a bunch of files from log1 to log164. I'm trying to LIST the directory (sorted) in a UNIX terminal but the sort functions are only providing the format like this: home:logs Home$ ls -1 | sort log1.gz log10.gz log100.gz log101.gz log102.gz log103.gz log104.gz log105.gz log106.gz ...etc What I want is home:logs Home$ ls -1 | sort log1.gz log2.gz log3.gz log4.gz log5.gz log6.gz log7.gz ...{more here} log99.gz log100.gz log101.gz log102.gz ...etc Any suggestions in what I could use to do this?
Rabiani
Mar 9, 2012, 02:18 AM • Last activity: Mar 7, 2025, 02:38 PM
5 votes
3 answers
1571 views
Is it ok that find displays double forward-slash?
Is it a bug that, when I `find ./path/here/` I get: ./path/here//foo ./path/here//bar I know `find` wants me to specify the path without the trailing slash, but surely it can detect the path that tab-completing leaves me with and adjust its output accordingly. Is there any reason why it doesn't?
Is it a bug that, when I find ./path/here/ I get: ./path/here//foo ./path/here//bar I know find wants me to specify the path without the trailing slash, but surely it can detect the path that tab-completing leaves me with and adjust its output accordingly. Is there any reason why it doesn't?
Bobby Jack (349 rep)
Nov 1, 2016, 11:36 PM • Last activity: Feb 25, 2025, 01:34 PM
2 votes
2 answers
774 views
Create folders with unique names of filenames
I am trying to create folders from unique filenames. Here are the files: T001 └── 20000101 ├── 6_3D-MP-RAGE_97.dcm ├── 6_3D-MP-RAGE_98.dcm ├── 5_133_VOLUMES_99027.dcm ├── 5_133_VOLUMES_99028.dcm ├── 5_133_VOLUMES_99029.dcm ├── 5_133_VOLUMES_99030.dcm ├── 4_COR_3D_T1_MPR_1.dcm ├── 4_COR_3D_T1_MPR_107...
I am trying to create folders from unique filenames. Here are the files: T001 └── 20000101 ├── 6_3D-MP-RAGE_97.dcm ├── 6_3D-MP-RAGE_98.dcm ├── 5_133_VOLUMES_99027.dcm ├── 5_133_VOLUMES_99028.dcm ├── 5_133_VOLUMES_99029.dcm ├── 5_133_VOLUMES_99030.dcm ├── 4_COR_3D_T1_MPR_1.dcm ├── 4_COR_3D_T1_MPR_107.dcm I want bash magic to result in this: T001 └── 20000101 ├── 6_3D ├── 6_3D-MP-RAGE_97.dcm ├── 6_3D-MP-RAGE_98.dcm ├── 5_133 ├── 5_133_VOLUMES_99027.dcm ├── 5_133_VOLUMES_99028.dcm ├── 5_133_VOLUMES_99029.dcm ├── 5_133_VOLUMES_99030.dcm ├── 4_COR ├── 4_COR_3D_T1_MPR_1.dcm ├── 4_COR_3D_T1_MPR_107.dcm And I have tried this code to extract folder names, but got stuck:
all_patterns=()
for file in sorted/folder/*/*
do
    name=${file##*/}
    pattern=echo ${name} | cut -f1 -d '_'
    all_patterns+=( "$pattern" )
done
Relyativist (139 rep)
Mar 17, 2020, 03:54 PM • Last activity: Feb 20, 2025, 12:06 PM
1 votes
4 answers
1498 views
How can I get the absolute path of a file deep within a directory?
Here's an example of directory `level0` contents in a tree-like format. However, suppose it is huge and contains many files as I am omitting many of them here: $ tree level0 level0 └── level1 └── level2 └── sample.txt I tried using `grep` to check some file with grep, but this is the output I receiv...
Here's an example of directory level0 contents in a tree-like format. However, suppose it is huge and contains many files as I am omitting many of them here: $ tree level0 level0 └── level1 └── level2 └── sample.txt I tried using grep to check some file with grep, but this is the output I received: $ tree /tmp/level0 | grep sample └── sample.txt However, I expect the output to be the actual absolute path /tmp/level0/level1/level2/sample.txt. How can I get the file's absolute path deep within a directory using a command?
showkey (499 rep)
Feb 13, 2025, 09:02 AM • Last activity: Feb 16, 2025, 10:07 AM
Showing page 1 of 20 total questions