簡體   English   中英

bash-如何檢測我的文件是否在(子)文件夾中

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

我正在嘗試編寫腳本以在bash中使用文件后移動文件。 我正在使用“用...打開”腳本,因此$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

foo的結果可能是:

/title_subdirectory/file with_spaces or_not.ext

要么

/file with_spaces or_not.ext

如何檢測文件是否在子目錄中,然后移動所有子目錄和文件?

我要檢查文件名是否與basename名相同(不帶目錄的名稱)

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

如果根目錄中的文件帶有前導/則在比較之前,您可能需要這樣做以除去前導/

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