[英]How to append the timestamp of a process to the name of a file in Unix
I have a directory /home/user/ingest/
which has many files inside it with varying sizes. 我有一个目录
/home/user/ingest/
,里面有许多大小不一的文件。 I need to move every file inside this folder into /home/user/ingest/inbox
every two minutes. 我需要每两分钟将此文件夹中的每个文件移动到
/home/user/ingest/inbox
。 There might be a situation where the first file might take more than 2 mins to move into /home/user/ingest/inbox
. 可能存在第一个文件可能需要2分钟以上才能移至
/home/user/ingest/inbox
。 In which case when its moving the second file, it SHOULDN'T take the first file as well. 在这种情况下,当移动第二个文件时,它也不应获取第一个文件。 Which is why I thought to append the timestamp to the name of files at the time of moving.
这就是为什么我考虑在移动时将时间戳添加到文件名的原因。 Is there anyway to do this in shell scripting?
无论如何,在shell脚本中可以做到这一点吗?
And I also understand that usingh crontab might help me in scheduling the execution of the shell script every two minutes. 而且我也了解使用crontab可能有助于我每两分钟安排一次shell脚本的执行。 I have a basic idea of cron but for my particular requirement, how can I check whether the file is being loved every two minutes or not?
我对cron有一个基本的了解,但是对于我的特定要求,如何每两分钟检查一次该文件是否被爱过?
You can do that with this bash script: 您可以使用以下bash脚本来做到这一点:
#!/bin/bash
TIMESTAMP=$(date +%s)
for f in /home/user/ingest/*
do
if [ -f "$f" ]; then
name=$(basename "$f")
mv $f /home/user/ingest/inbox/${name}_${TIMESTAMP}
fi
done
The ${TIMESTAMP}
is the number of seconds since the epoch (Jan 1970). ${TIMESTAMP}
是从纪元(1970年1月)以来的秒数。 The for loop iterates through everything in the /home/user/ingest/
directory, and the if statement checks to see if the file is a regular file (not directory, not symlink), and then the file is moved to /home/user/ingest/inbox/
with the timestamp appended to the end. for循环遍历
/home/user/ingest/
目录中的所有内容,并且if语句检查文件是否为常规文件 (不是目录,不是符号链接),然后将文件移至/home/user/ingest/inbox/
,并将时间戳记附加到末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.