尝试获取带有空格的文件名和文件夹名

时间:2019-07-26 09:14:47

标签: linux shell dirname basename

我正在尝试获取我的文件夹名称和文件名(不带扩展名,但是我发现不带扩展名)。 如果我可以进入数组,那会很棒,但我仍在研究空间问题。 它仅获取父文件夹名称和文件名,包括文件夹名称中的空格。

例如)

bin/data/ex/1.json
bin/data/2.json
bin/data fix/3.json (there is a space between data and fix, 'data fix')
bin/4.json

结果)

ex_1
data_2
data fix_3 or datafix_3 (with space or no space is fine)
bin_4

实际上是我的数据)

./123, 1/2.json
./ex/1.json

想要的结果)

 123, 1_2 or 123,1_2
 ex_1

这是我的密码

for i in $(find . -iname "*.json")
#find *.json under folder and current folder
do
        echo "i(.json) is $i"
# TEst Print  .json file 
        dr="$(basename "$(dirname "$i")")"
        echo "dr is $dr"
# Test pring $dr
        name=$(sudo basename $i .json);
# Test print file name
        echo "name is $name"

        echo "foldername_filename is $dr _$name"
        echo "foldername_filename2 is $(basename "$(dirname "$i")")_$(basename $i .json)"
done

但是我知道了

i(.json) is ./123,
dr is .
name is 123,
foldername_filename is . _123,
foldername_filename2 is ._123,
i(json) is 1/2.json
dr is 1
name is 2
foldername_filename is 1 _2
foldername_filename2 is 1_2
i(json) is ./ex/1.json
dr is ex
name is 1
foldername_filename is ex _1
foldername_filename2 is ex_1

0 个答案:

没有答案