Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4027544
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:06:35+00:00 2026-05-20T11:06:35+00:00

I’ve made a bash script which is executed as root and must verify the

  • 0

I’ve made a bash script which is executed as root and must verify the existence of a file in the Desktop of an user. Here is the code:

if [ -e /Users/*/Desktop/file ]; then
....

The fact is if there are multiple users in /Users/, and that the file is not present in the first user (by alphabetical order) but in one of the others, it will fail.

So to resolve the issue, I thought this was good:

if [ -e ~/Desktop/file ]; then
....

but there, as the script is executed as root, ~/Desktop/ is /private/var/root/Desktop instead of /Users/my_user/Desktop .

I’m not very at ease for the moment with bash so I hope you will help me to resolve this issue ! Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:06:35+00:00Added an answer on May 20, 2026 at 11:06 am

    You could use a for loop in bash to test for each user’s home directory in turn, e.g.:

    for d in /Users/*
    do
        if [ -e "$d/Desktop/file" ]
        then
            echo "The file existed under $d"
        else
            echo "The file was not found under $d"
        fi
    done
    

    The /Users/* is expanded into /Users/alice /Users/bob /Users/charles (or whatever) and then $d is given the value of each of those in turn inside the loop. Obviously you can replace the echo statments with whatever you want to do in either case.

    The bash manual’s section on looping constructs, including for loops, is here – that might not be the best place to start, but you can find plenty of examples with Google.

    One thing to note about this answer is that if you’re being strict, a user’s home directory doesn’t have to be called /Users/username – strictly speaking you need to look in /etc/passwd to find all users and their corresponding home directories. However, for what you want to do, this might be enough.


    Update 1: It sounds from the comment below as if you want to take an action if no file matching /Users/*/Desktop/file exists for any user. This sounds surprising to me, but if you really want to do that, you could use one of the recipes from this other Stack Overflow question, for example:

    if [ -z "$(shopt -s nullglob; echo /Users/*/Desktop/file)" ]
    then
        echo "No one had a file in ~/Desktop/file"
    fi
    

    Update 2: Your comments on Dennis Williamson’s answer indicate that this script is expected to be run by non-root users on this machine. If that’s the case then I think his suggestion in the comments is the right one – you just want to test:

    if [ -e "$HOME/Desktop/file" ]
    then
        echo "Found the file"
    else
        echo "Failed to find the file"
    fi
    

    … and then you just just need to make sure you test it as a non-root user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.