What's the best way to check file permissions before moving files if needed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's the best way to check file permissions before moving files if needed?
# 1  
Old 07-14-2019
What's the best way to check file permissions before moving files if needed?

Hello,
I would like to know if it's a good practice to check the file permissions of the contents of a directory before moving them. For example:
Code:
mv -- "$directory"/* "$directory"/.[!.]* "$directory"/..?* "$destination"

The variables $directory and $destination contain the path to an existing directory. The problem is that I don't have control of the user's directory who runs the script so, in theory, the files may not have the needed permissions to be moved, and the directory is required to be empty. What's a good way to check if every file was moved successfully?

The first thing that came to mind is to check the exit status of the command:
Code:
mv -- "$directory"/* "$directory"/.[!.]* "$directory"/..?* "$destination" || exit 1

However, this doesn't mean a file couldn't be moved, because it can also exit because of a non-existing directory entry as a result of the glob. I also thought of checking if the directory is actually empty:
Code:
for entry in "$directory"/* "$directory"/.[!.]* "$directory"/..?*
do
    if [ -e "$entry" ] || [ -L "$entry" ]
    then
        printf "A file couldn't be moved.\n" 1>&2
        exit 1
    fi
done

But I'd like to know if this check is unnecessary. I also thought about checking if every single file has the needed permissions before using the mv command, but I'm afraid that would be superfluous.
Thanks in advance.

Last edited by Cacializ; 07-14-2019 at 04:32 PM..
# 2  
Old 07-14-2019
You should ALWAYS check user input - consider it toxic if that helps.

If you use linux the -empty test works for what you want - you do not mention your system or shell
i.e.,
Code:
# check if it is a directory && check check empty "" need in case the $directory variable has spaces 
if [[ -d /path/to/directory"$directory"  &&  find /path/to/directory -type d -name "$directory"  -empty ]] ; then
      echo "ok"
else
     echo "not ok"
fi

You can also use the ls command more globally

Code:
if [ -z "$(ls -A $directory)" ]; then
   echo "Empty"
else
   echo "Not Empty"
fi

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 07-15-2019
In order to move a file the directory is modified i.e
must be writable. The file permissions do not really matter, while in an interactive shell the mv might become interactive i.e. ask.
So either test
Code:
if [ -w "$directory" ] && [ -w "$destination" ]
then
  mv -v ... "$destination/"
else
  echo "dir not writable!"
fi

Or make them writable
Code:
if chmod u+w "$directory/" "$destination/"
then
  mv -v ... "$destination/"
else
  echo "dir not writable"
fi

A trailing / will automatically follow a symlink and test for a directory.
The chmod and the -d test always follow a symlink.
(Nevertheless the chmod targets should have trailing / to ensure they are not files.)
The -v (verbose) option can be captured in a file or a variable.

--- Post updated at 10:26 ---

Quote:
Originally Posted by jim mcnamara
You should ALWAYS check user input - consider it toxic if that helps.

If you use linux the -empty test works for what you want - you do not mention your system or shell
i.e.,
Code:
# check if it is a directory && check check empty "" need in case the $directory variable has spaces 
if [[ -d /path/to/directory"$directory"  &&  find /path/to/directory -type d -name "$directory"  -empty ]] ; then
      echo "ok"
else
     echo "not ok"
fi

Won't work correctly - find does not give a suitable exit status!
EDIT: now I see the find is within the [[ ]] - won't run unless it is in $( ).
I try a correction:
Code:
if [[ -n $(find -maxdepth 0 -type d "$directory" -empty) ]]; then echo "$directory is an empty directory."; fi

Note: unlike the [[ ]], in [ ] you should quote the sub shell's output "$( )".

Last edited by MadeInGermany; 07-15-2019 at 05:30 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-15-2019
Quote:
Originally Posted by jim mcnamara
You should ALWAYS check user input - consider it toxic if that helps.
This is something I always do. Like I said, the variables contain the path to existing directories. They're checked before using the mv, but I skipped that part in order to focus on the permissions question.


Quote:
Originally Posted by jim mcnamara
You should ALWAYS check user input - consider it toxic if that helps.

If you use linux the -empty test works for what you want - you do not mention your system or shell
You're right, I'm sorry I didn't mention it. My shell is Bash, and I use GNU coreutils, but the script is meant to be POSIX-compliant, hence the use of the single square bracket test.


Quote:
Originally Posted by jim mcnamara
You can also use the ls command more globally

Code:
if [ -z "$(ls -A $directory)" ]; then
   echo "Empty"
else
   echo "Not Empty"
fi

I know this is off-topic, but this is also one of the cases I don't know what the best practice is, because the standard states that the -A "writes out all directory entries, including those whose names begin with a <period> ( '.' ) but excluding the entries dot and dot-dot (if they exist)". But then there's the HP-UX ls, where "for a user with appropriate privileges, this flag defaults to on, and is turned off by -A". What should I do in this case?


Quote:
Originally Posted by MadeInGermany
In order to move a file the directory is modified i.e
must be writable. The file permissions do not really matter, while in an interactive shell the mv might become interactive i.e. ask.
So either test
Code:
if [ -w "$directory" ] && [ -w "$destination" ]
then
  mv -v ... "$destination/"
else
  echo "dir not writable!"
fi

Or make them writable
Code:
if chmod u+w "$directory/" "$destination/"
then
  mv -v ... "$destination/"
else
  echo "dir not writable"
fi

A trailing / will automatically follow a symlink and test for a directory.
The chmod and the -d test always follow a symlink.
(Nevertheless the chmod targets should have trailing / to ensure they are not files.)
The -v (verbose) option can be captured in a file or a variable.
I see. But if the variables are checked with [ -d "$directory" ] && [ -d "$destination" ], the railing slash is not needed, right?
# 5  
Old 07-16-2019
With this method, the mask for file names will not be needed
Code:
[ "$(ls -A "$destination")" ] && exit
mv $(compgen -f "$derectory"/) "$destination"

# 6  
Old 07-17-2019
Quote:
Originally Posted by Cacializ
...
But if the variables are checked with [ -d "$directory" ] && [ -d "$destination" ], the railing slash is not needed, right?
Right. A trailing / would cause a double-check. Is technically okay though.

For Posix-compatibility you should stick to "$(ls -A)" and test it for being null or non-null. Generally better for correctly counting crazy filenames is "$(ls -qA)".
In HP-UX you can do
Code:
UNIX95=1; export UNIX95

that tells many commands to become Posix-compliant.
In Solaris you can prefix /usr/xpg4/bin to the PATH. The following should work pretty much everywhere:
Code:
PATH=/usr/xpg4/bin:/bin:/usr/bin:/usr/sbin:/sbin UNIX95=1
export PATH UNIX95
# all Posix commands and options should work now

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 07-17-2019
You might also consider the test options for files (man test for more detail)

Another alternative might be to use stat to get the value you need. Jim is right though, you don't just need to check the files, but the directory permissions too to ensure you can write to them. The update needed will create or remove the ile as part of the move. It can also be a nice way to get around sensitive data input in a particular place that supposedly has been locked down, i.e. the file payroll-input.txt might have RW for the owner, Read for a group you are not in and nothing for the rest, but if you have access to update the directory, you can remove or rename the file then create your own.

Smilie I'm not advocating it, but it is something consider and make everyone panic about simple security Smilie



The output from stat -c '%a %U %G' might give you the basics of what you need and you might parse them to decide if you should continue.



There are, however, lots of probably better suggestions before mine, such as that from MadeInGermany.



Just more options.

Kind regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving alphanumeric files according to the digit in file name

This is the content of my directory c_g_se1_gb.ph c_g_se1_gb.ph_pl_s.t c_g_se1_gb.ph_pl_tr.t c_g_se3_gb.ph c_g_se3_gb.ph_pl_s.t c_g_se3_gb.ph_pl_tr.t c_g_se2_gb.ph c_g_se2_gb.ph_pl_s.t c_g_se2_gb.ph_pl_tr.t c_g_se4_gb-1.ph c_g_se4_gb-1.ph_pl_s.t c_g_se4_gb-1.ph_pl_tr.t... (9 Replies)
Discussion started by: sammy777888
9 Replies

2. UNIX for Dummies Questions & Answers

Is there a way to check when the permissions for the file got changed in AIX

Is there a way to check when the permissions for the file got changed in AIX IS there some file which logs all these details? Best regards, Vishal (4 Replies)
Discussion started by: Vishal_dba
4 Replies

3. Shell Programming and Scripting

Moving files based on file name

Hi All, I have multiple files in the folder, I want to move those files into the other folder on based of name File names: Template_server1_01==> Template_server1_02==>To one directory /Server1 Template_server1_03==> Template_server2_01==> Template_server2_02==>To one... (9 Replies)
Discussion started by: sharsour
9 Replies

4. Shell Programming and Scripting

To check the file permissions using python scripting

Hi, For a particular set of files, am trying to check if they are writable. i.e., checking whether they are having permissions greater than 755. Am able to check this using the statement: "if (os.path.isfile(FILE_PATH) and (os.stat(FILE_PATH).st_mode & 0777) == 0777):" But the problem... (1 Reply)
Discussion started by: arjun_arippa
1 Replies

5. Programming

To check the file permissions using python scripting

Hi, For a particular set of files, am trying to check if they are writable. i.e., checking whether they are having permissions greater than 755. Am able to check this using the statement: "if (os.path.isfile(FILE_PATH) and (os.stat(FILE_PATH).st_mode & 0777) == 0777):" But the problem here... (0 Replies)
Discussion started by: arjun_arippa
0 Replies

6. Shell Programming and Scripting

Moving files based on file creation

Hi, I have a directory having so many number of files. Now I want to move the files which are older than one month (lets say) from this directory to another directory (say BKP dir). Simply, if file is olderthan one month move it from source1 dir to BKP1 dir. My file names doesn't have... (7 Replies)
Discussion started by: karumudi7
7 Replies

7. Shell Programming and Scripting

Moving files only by oldest file one at a time

Hi I am want to create a script where the file gets moved from the current folder to a folder transfer based on the oldest first. This script should run one file at a time using a loop. I want it as a loop because I want to do some processing while I have one file. Can anyone guide me on this? (2 Replies)
Discussion started by: chamajid
2 Replies

8. Shell Programming and Scripting

How to check file permissions from a script.

hello, I have to write a script to run the other script inside it.So iam planning to write like this? first check the perimissions of the file. Alogorthim ---------- if(!filepermissions == execute) then echo" Permissions denined" else execute the script. file name is : load_mf.sh... (1 Reply)
Discussion started by: rajkumar_g
1 Replies

9. Shell Programming and Scripting

Help needed with searching files and moving them to a different directory

I am new to shell scripting. Can someone help me out with this one please? I need to write a script fot the following scenario: I am currently in /parent directory. I have a set of files in /parent/error_files directory My script has to search for a file in /parent/erratic_files... (1 Reply)
Discussion started by: ss3944
1 Replies

10. Shell Programming and Scripting

Using grep - check the permissions of the file searched

What I need to do is: I need to use the grep command to search for pattern in directory and sub-directories. And also I need to show the permission of file been seached by the grep command. Could any one please suggest me? ----------------- $> cat file1.txt A -----------------... (8 Replies)
Discussion started by: Johny001
8 Replies
Login or Register to Ask a Question