Guillaume
Guillaume

Reputation: 2918

bash - How to detect if my file is in a (sub)folder

I'm trying to write a script to move files after use them in bash. I'm using "open with" my script, so $1 is the file (fullpath)

#! /bin/bash

fullpath="$1"
rootpath="/home/guillaume/Videos"
foo=${fullpath#${rootpath}}
echo "$foo"

zenity --question --title="Déplacer le fichier ?" --text="Déplacer le fichier vers le dossier VU ?"
if [ $? = 0 ]
then
    if # foo don't have a folder
    then
       mv $rootpath"$foo" /home/guillaume/Videos/VU
    elif # foo contains a folder
       # command to copy the subdirectory
    fi
fi

The result for foo could be:

/title_subdirectory/file with_spaces or_not.ext

or

/file with_spaces or_not.ext

How to detect if the file is in a subdirectory, and then, move all the subdirectory and the files ?

Upvotes: 0

Views: 35

Answers (1)

Philip Couling
Philip Couling

Reputation: 14873

I'd check if the file name is the same as it's basename (name without directory)

if [ `basename "$foo"` = "$foo" ] ; then
  echo root directory
else
  echo subdirectory
fi

If files in your root directory have a leading / then you may need to do it like this to strip the leading / before comparison:

if [ `basename "$foo"` = "${foo#/}" ] ; then
  echo root directory
else
  echo subdirectory
fi

Upvotes: 1

Related Questions