简体   繁体   English

bash-如何检测我的文件是否在(子)文件夹中

[英]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. 我正在尝试编写脚本以在bash中使用文件后移动文件。 I'm using "open with" my script, so $1 is the file (fullpath) 我正在使用“用...打开”脚本,因此$1是文件(完整路径)

#! /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: foo的结果可能是:

/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 ? 如何检测文件是否在子目录中,然后移动所有子目录和文件?

I'd check if the file name is the same as it's basename (name without directory) 我要检查文件名是否与basename名相同(不带目录的名称)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM