是否可以将剪切命令的结果发送到预定义的目录?
Is it possible to send the result of a cut command to a pre defined directory?
我目前正在学习如何编写 shell 脚本,作为作业的一部分,我需要根据文件中的日期将提供给我的文件分类到不同的目录中。
日期在文件的第一行,所有功能都必须在同一个脚本中。
我现在的想法是翻译成需要的格式,然后用mkdir -p
函数创建多个目录,然后用cut
到select我要的日期部分想要突出显示数据和 return 它们,理想情况下我现在希望能够从 SelectYear
、SelectMonth
和 SelectDay
函数中获取这些输出并将这些文件放入我已经使用 CreateAllDirectories
函数设置的相应目录。
这可能吗?
这是我需要用这个脚本实现的最终结果,为文件中出现的每一年创建一个目录,然后在每年的目录中为月份创建另一个目录,然后在月份目录中有一个日期目录,然后是包含其中确切日期的所有文件的列表,如下所示:
[~/filesToSort] $ ls -R
.:
2000 2001 2002 2003 2004 2005 2006 2007 2008 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
./2000:
02 03 04 09 10 11 12
./2000/02:
09
./2000/02/09:
ff_1646921307 ….
目前这是我的脚本:
#!/bin/bash
#Changes the date format from YYYY-MM-DD to YYYY/MM/DD
function ChangeSeperater{
head -n 1 ~/filesToSort/ff_* | tr '-' '/'
}
#Makes multiple directories
function CreateAllDirectories{
mkdir -p /year/month/day
}
#Cuts year from file
function SelectYear{
head -n 1 ~/filesToSort/ff_* | cut -c1-4
}
#Cuts month from file
function SelectMonth{
head -n 1 ~/filesToSort/ff_* | cut -c6-7
}
#Cuts day from file
function SelectDay{
head -n 1 ~/filesToSort/ff_* | cut -c9-10
}
编辑:感谢您的帮助!
如果有人感兴趣,这是完成的脚本:
#!/bin/bash
#Changes the date format from YYYY-MM-DD to YYYY/MM/DD
#Change Seperator function, gets the date from its parameter, changes the date from YYYY-MM-DD to YYYY/MM/DD
function ChangeSeperator() {
echo "" | tr '-' '/'
}
#Sorts the files into the correct directories, cuts the entire date from the file and turns it into a directory, uses the ChangeSeperator function from earlier make the parent directory and all sub directories
for file in ~/filesToSort/ff_*
do
directory=$(ChangeSeperator $(head -c 10 "$file"))
mkdir -p "$directory"
mv "$file" "$directory"
done
首先,您可能需要在某处使用循环来筛选所有文件,您可能会考虑一个一个地处理它们。
至于日期,您可能应该在文档中查找可以为您提供大部分信息的日期。
举例:
date -d 2018-07-01 +"%Y/%m/%d"
2018/07/01
顺便说一句,你总是可以做类似的事情:
d=$(date -d 2018-07-01 +"%Y/%m/%d")
echo "d="$d
d=2018/07/01
mkdir -p $d
希望这是足够的指导......不是在这里做你的作业:)
您不需要所有这些功能,只需要将日期从 yyyy-mm-dd
转换为路径名 yyyy/mm/dd
的功能就足够了。
for file in ~/filesToSort/ff_*
do
directory=$(ChangeSeperator $(head -c 10 "$file"))
mkdir -p "$directory"
cp "$file" "$directory"
done
ChangeSeperator
函数需要从其参数中获取日期:
ChangeSeperator() {
echo "" | tr '-' '/'
}
我目前正在学习如何编写 shell 脚本,作为作业的一部分,我需要根据文件中的日期将提供给我的文件分类到不同的目录中。
日期在文件的第一行,所有功能都必须在同一个脚本中。
我现在的想法是翻译成需要的格式,然后用mkdir -p
函数创建多个目录,然后用cut
到select我要的日期部分想要突出显示数据和 return 它们,理想情况下我现在希望能够从 SelectYear
、SelectMonth
和 SelectDay
函数中获取这些输出并将这些文件放入我已经使用 CreateAllDirectories
函数设置的相应目录。
这可能吗?
这是我需要用这个脚本实现的最终结果,为文件中出现的每一年创建一个目录,然后在每年的目录中为月份创建另一个目录,然后在月份目录中有一个日期目录,然后是包含其中确切日期的所有文件的列表,如下所示:
[~/filesToSort] $ ls -R
.:
2000 2001 2002 2003 2004 2005 2006 2007 2008 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
./2000:
02 03 04 09 10 11 12
./2000/02:
09
./2000/02/09:
ff_1646921307 ….
目前这是我的脚本:
#!/bin/bash
#Changes the date format from YYYY-MM-DD to YYYY/MM/DD
function ChangeSeperater{
head -n 1 ~/filesToSort/ff_* | tr '-' '/'
}
#Makes multiple directories
function CreateAllDirectories{
mkdir -p /year/month/day
}
#Cuts year from file
function SelectYear{
head -n 1 ~/filesToSort/ff_* | cut -c1-4
}
#Cuts month from file
function SelectMonth{
head -n 1 ~/filesToSort/ff_* | cut -c6-7
}
#Cuts day from file
function SelectDay{
head -n 1 ~/filesToSort/ff_* | cut -c9-10
}
编辑:感谢您的帮助! 如果有人感兴趣,这是完成的脚本:
#!/bin/bash
#Changes the date format from YYYY-MM-DD to YYYY/MM/DD
#Change Seperator function, gets the date from its parameter, changes the date from YYYY-MM-DD to YYYY/MM/DD
function ChangeSeperator() {
echo "" | tr '-' '/'
}
#Sorts the files into the correct directories, cuts the entire date from the file and turns it into a directory, uses the ChangeSeperator function from earlier make the parent directory and all sub directories
for file in ~/filesToSort/ff_*
do
directory=$(ChangeSeperator $(head -c 10 "$file"))
mkdir -p "$directory"
mv "$file" "$directory"
done
首先,您可能需要在某处使用循环来筛选所有文件,您可能会考虑一个一个地处理它们。
至于日期,您可能应该在文档中查找可以为您提供大部分信息的日期。
举例:
date -d 2018-07-01 +"%Y/%m/%d"
2018/07/01
顺便说一句,你总是可以做类似的事情:
d=$(date -d 2018-07-01 +"%Y/%m/%d")
echo "d="$d
d=2018/07/01
mkdir -p $d
希望这是足够的指导......不是在这里做你的作业:)
您不需要所有这些功能,只需要将日期从 yyyy-mm-dd
转换为路径名 yyyy/mm/dd
的功能就足够了。
for file in ~/filesToSort/ff_*
do
directory=$(ChangeSeperator $(head -c 10 "$file"))
mkdir -p "$directory"
cp "$file" "$directory"
done
ChangeSeperator
函数需要从其参数中获取日期:
ChangeSeperator() {
echo "" | tr '-' '/'
}