Bash script error trying to write to file: no such file or directory
Check that generated filename again:
/home/user/db_dump_12/02/2017_14:30:15.txt
It tries to write into a subdirectory called 02
of db_dump_12
. If these directories don't exist, you'll get a "No such file or directory" error.
I suggest using another date format:
dumpfile_date=$( date +"%F-%T" ) # or %Y%m%d-%H%M%S or something similar
pg_dumpall -U postgres -w > "/home/user/db_dump_$dumpfile_date.txt"
Related videos on Youtube
rijo79
Updated on September 18, 2022Comments
-
rijo79 almost 2 years
I'm getting an error with a very simple script. The script should run pg_dumpall and write the output to a file but I'm getting an error that the resulting file doesn't exist. I know it doesn't exist, thats why I want to creat it! :-/
#!/bin/bash #remove previous day's dump and create new rm -f /home/user/db_dump_*.txt; pg_dumpall -U postgres -w > /home/user/db_dump_`date '+%d/%m/%Y_%H:%M:%S'`.txt;
The result is:
db_dump.sh: line 4: /home/user/db_dump_12/02/2017_14:30:15.txt: No such file or directory
The date command is working fine and the filename comes out perfect but instead of the file being created, I get the error saying that it doesn't exist. I'm running the script as root so there should be no issues with permissions. I can write to the user's home directory without any problems.
Any ideas?
-
Celada over 7 yearsSince your filename contains slashes, try pre-creating the directories involved before trying to write to a file inside them. In this instance,
mkdir -p /home/user/db_dump_12/02
. On the other hand, I urge you to reconsider your directory structure as it doesn't seem logical to, say, keep all of the files written on the 12th of any month in a given directory. Perhaps if your date format were big-endian instead oflittle-endian it might make more sense.
-
-
rijo79 over 7 yearsderp... Yeah, that'll do it... Slashes in the date are gonna be read as a path...
-
rijo79 over 7 yearsI just replaced the slashes with hyphens in the date parameters and it works the way I want. Thanks!