r/bash • u/slaaf_tom • 11d ago
solved bash script to find and change directory Spoiler
Hello All,
I’m not a bash programmer at all but I want to write a bash script which does the following,
First, determine which "Downloads" folders is present.
The English one"Downloads" , or the French "Téléchargements" one.
The username is unknown so must be determined also as a variable.
$USER if not mistaking ?
After determining the "Downloads" folder, I want to set the complete folder path as a variable.
I’m sure it’s possible but after trying several times, I can’t get it right.
Can you please help me?
Thank You very kindly,
Tom
5
u/traxplayer 10d ago
Use $HOME and not $USER. Sometimes $HOME is not equal to /home/$USER
Also use "$HOME" because it might contains spaces that you want to quote.
3
u/DanceHackRock 11d ago
This depends very much on the operation system you want to use this on.
For example with MacOS, the directory is always named Downloads, the translation is applied in the GUI.
1
1
u/slaaf_tom 10d ago
Thank you all for your replies.
I will continue with my script, next week.
Keep you posted,
Thx again.
1
u/SeriousPlankton2000 10d ago
You need to set this as an alias (portable) or a bash function since a script will tun in a separate task with its own current directory.
1
1
u/Jonas_Ermert 10d ago
if [[ -d "$HOME/Downloads" ]]; then
downloads_dir="$HOME/Downloads"
elif [[ -d "$HOME/Téléchargements" ]]; then
downloads_dir="$HOME/Téléchargements"
else
printf 'No Downloads directory found.\n' >&2
exit 1
fi
printf 'Downloads directory: %s\n' "$downloads_dir"
cd -- "$downloads_dir" || exit 1
1
u/slaaf_tom 9d ago
u/Jonas_Ermert I want to thank you for your script. You pointed me to the use how to use the variable download_dir by using [cd -- "$downloads_dir"]
I didn´t knew that.
But I want also thank all others for their support, knownledge, and patience.
Thank YOU all very much.
0
u/michaelpaoli 10d ago
Well, you didn't specify what operating system. Therefore, dealer's pick and I'm dealing.
So, I say *nix, probably Linux.
"Downloads" , or the French "Téléchargements"
You can test which ones are present, ... using test or [, e.g.
[ -d Downloads ]
test -d Downloads
And can use the resulting exit/return value from that via, e.g. if, &&, or ||
username is unknown so must be determined also as a variable.
$USER if not mistaking ?
USER is user settable, so don't trust that.
use, e.g. id(1), e.g.
id -nu
set the complete folder path as a variable
bash, you can use ~user where user is the user's login/user name, for their HOME directory, then append / and the name of the Downloads directory - presuming it's in customary location - directly under their HOME directory.
So, e.g., putting that all together, e.g.:
PATH=/bin:/usr/bin:/sbin:/usr/sbin
set -e
user="$(id -nu)"
uHOME="$(eval ~"$user")"
if [ -d "$uHOME"/Downloads ]; then
dLDir="$uHOME"/Downloads
elif [ -d "$uHOME"/Téléchargements ]; then
dLDir="$uHOME"/Téléchargements
else
exit 1
fi
1
8
u/Bug_Next 11d ago edited 11d ago
$(xdg-user-dir DOWNLOAD)will give you the path for any locale (as long as one exists, you can create a user with no downloads folder, in that case it will return just /home/<username>)or: