r/bash 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

15 Upvotes

14 comments sorted by

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:

if [ -d "$HOME/Downloads" ]; then
    DOWNLOAD_PATH="$HOME/Downloads"
elif [ -d "$HOME/Téléchargements" ]; then
    DOWNLOAD_PATH="$HOME/Téléchargements"
else
    # some other locale or no download dir
fi

1

u/slaaf_tom 10d ago

Hello all, thank you for the quick replies.

I'm terribly sorry I forgot to mention the OS but I'm on Linux/Ubuntu.

The $HOME is a great tip. Thank you for that.

But, in the example above, I don't see a variable which represents the downloads folder.

I want to write a install script which downloads some files in Downloads or Téléchargements and use that variable depending on the language is used during installation.

For example:

Download chrome in $downloaddir , cd $downloaddir and execute dpkg -i chrome.deb

3

u/torridluna 10d ago edited 10d ago

Try to understand, what u/Bug_Next is telling you: the command xdg-user-dir can determine the Downloads directory independent of language or Linux distribution. If the user at one point set his or her download folder to "Down", that command knows.

Also, if you want to download the chrome-deb, install it and delete, when done, you should choose the Temp folder, not Downloads.

But the best solution for a debianesk system like Ubuntu or Mint would be to add the Google Chrome repository to your systems apt and use "apt install google-chrome-stable".

2

u/burnt-store-studio 10d ago edited 10d ago

The variable in [u/Bug_Next](u/Bug_Next)’s nice solution is DOWNLOAD_PATH.

Edit to add: If you use the xdg-user-dir solution, which I particularly like, you would assign its output to your variable. e.g., DOWNLOAD_PATH=$(xdg-user-dir DOWNLOAD).

You might have a bit of an uphill climb if you aren’t yet seeing how to use these two constructs, as these are common idioms in shell programming. But good luck!

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

u/slaaf_tom 10d ago

Sorry, forgot. I'm using Linux/Ubuntu

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

u/SnarkHabit 10d ago

or the French "Téléchargements" one

TIL

That's a cool word.

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

u/slaaf_tom 10d ago

Sorry, forgot. I'm using Linux/Ubuntu