这里小编给大家分享一些Shell脚本实现根据文件的修改时间来分类文件(共含6篇),方便大家学习。同时,但愿您也能像本文投稿人“喵喵然”一样,积极向本站投稿分享好文章。
这篇文章主要介绍了Shell脚本实现根据文件的修改时间来分类文件,本文直接给出实现代码,需要的朋友可以参考下
#!/bin/bash# exctute# ./mod.sh file_type input_folder output_folder# ./mod.sh *.txt /tmp /data/# paramater count if [ ! $# -eq 3 ]; then echo “[ERROR] error paramater.” exitfi# file typefile_type=“${1}”# input foloderif [ -d “${2}” ]; then folder=“${2}”else echo “[ERROR] input folder is not exsit.” exit fi# output folderif [ -d “${3}” ]; then utput=“${3}”else echo “[ERROR] output folder is not exsit.” exitfi# search filefind ${folder} -name “${file_type}” | while read filename ; do # file type file_type=`echo ${filename##*.}` # file size file_size=`stat “${filename}” | sed -n ‘2,1p‘ | awk ‘{print $2}‘ ` # file modify time file_modify=`stat “${filename}” | sed -n ‘6,1p‘ | awk ‘{print $2, $3}‘ | sed -e ‘s/[-: ]//g‘ ` # output folder path=“${output}/${file_modify:0:6}” if [ ! -d “${path}” ]; then mkdir -p ${path} echo “folder(${path}) is created . ” fi # new file full name new_file_name=`echo ${path}/${file_modify}_[${file_size}].${file_type}` if [ ! -f “${new_file_name}” ]; then mv “${filename}” “${new_file_name}” else echo “file(${new_file_name}) is exsit, can not be removed. ” fidoneecho “finished !”exit
作者:vpsee 字体:[增加 减小] 类型:
这篇文章主要介绍了Shell脚本逐行读取文本文件,本文着重探讨不改变文本格式的方法读取出文件内容,需要的朋友可以参考下
网上有很多 shell script. 读文本文件的例子,但是都没有讲出故事的全部,只说了一半,举个例子,比如从一个 testfile 文件中读取如下格式的文本行:
代码如下:
$ vi testfile
ls -a -l /bin | sort
ls -a -l /bin | sort | wc
ls -a -l | grep sh | wc
ls -a -l
ls -a -l | sort | wc
最常见的一个 line by line 读取文件内容的例子就是:
代码如下:
$ vi readfile
#!/bin/sh
testfile=$1
while read -r line
do
echo $line
done < $testfile
$ chmod +x readfile
$ ./readfile testfile
ls -a -l /bin | sort
ls -a -l /bin | sort | wc
ls -a -l | grep sh | wc
ls -a -l
ls -a -l | sort | wc
这个例子的问题是读取文本行后,文本格式发生了变化,和原来 testfile 文件的内容不完全一致,空格字符自动被删除了一些。为什么会这样呢?因为 IFS,如果在 shell script. 里没有明确指定 IFS 的话,IFS 会默认用来分割空格、制表、换行等,所以上面文本行里多余的空格和换行都被自动缩进了。
如果想要输出 testfile 文件原有的格式,把每行(作为整体)原封不动的打印出来怎么办?这时需要指定 IFS 变量,告诉 shell 以 “行” 为单位读取,
代码如下:
$ vi readfile
#!/bin/sh
IFS=“”
testfile=$1
while read -r line
do
echo $line
done < $testfile
$ ./readfile testfile
ls -a -l /bin | sort
ls -a -l /bin | sort | wc
ls -a -l | grep sh | wc
ls -a -l
ls -a -l | sort | wc
上面两种方法的输出不是差不多吗,有什么关系呢,第一种还美观一些?关系重大,VPSee 昨天写了一个模拟 shell 的 C 程序,然后又写了一个 shell script. 来测试这个 C 程序,这个 script. 需要从上面的 testfile 里读取完整一行传给 C 程序,如果按照上面的两种方法会得到两种不同的输入格式,意义完全不同:
代码如下:
$./mypipe ls -a -l | sort | wc
$./mypipe “ls -a -l | sort | wc ”
显然我要的是第2种输入,把 “ls -a -l | sort | wc ” 作为整体传给我的 mypipe,来测试我的 mypipe 能不能正确识别出字符串里面的各种命令。
如果不用 IFS 的话,还有一种方法可以得到上面第二种方法的效果:
代码如下:
#!/bin/sh
testfile=$1
x=`wc -l $testfile |awk ‘{print $1}‘`
i=1
while [ $i -le $x ]
do
echo “`head -$i $testfile | tail -1`”
i=`expr $i + 1`
done
1.文件的时间属性查看,包括修改时间与创建时间;
2.文件修改时间的修改,精确到秒;
代码如下:
(Apache Tomcat/6.0.18下运行通过!)
<%@ page import=“java.io.*” %>
<%@ page import=“java.util.*, java.text.*” %>
<%@ page language=“java” import=“java.util.Enumeration” contentType=“text/html; charset=GB2312”%>
JSP timeshell by oldjun
<%!
public static String getFileCreateDate(File _file) {
File file = _file;
try {
Process ls_proc = Runtime.getRuntime.exec(“cmd.exe /c dir ”“ + file.getAbsolutePath() + ”“ /tc”);
BufferedReader br = new BufferedReader(new InputStreamReader(ls_proc.getInputStream()));
for (int i = 0; i < 5; i++) {
br.readLine();
}
String stuff = br.readLine();
StringTokenizer st = new StringTokenizer(stuff);
String dateC = st.nextToken();
String time = st.nextToken();
String datetime = dateC.concat(“ ”+time);
br.close();
return datetime;
} catch (Exception e) {
return null;
}
}
String folderReplace(String folder){
return folder.replace('\','/');
}
%>
<%
String action = null;
if (request.getParameter(“action”) == null)
action = “main”;
else
action = (String)request.getParameter(“action”);
if (action.equals(“main”)) {
%>
filepath:
(for instance C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/ROOT/time.jsp)
<%
}else if (action.equals(“getinfo”)) {
String filepath = folderReplace(request.getParameter(“file”));
File file = new File(filepath);
if(!file.exists()){
out.println(“”);
}
%>
filepath:
这篇文章主要介绍了Shell脚本中使用for循环和cat命令实现按顺序合并文件,本文先是用sed命令来实现,发现不能完成需求,后使用for循环解决,需要的朋友可以参考下
工作目录下面有mydoc1.txt,mydoc2.txt...mydoc41.txt,本来想用sed排列依次取值排序,然后用cat来合并这些文件,发现达不到预期效果,合并令如下所示:
代码如下:
ls -lF *.txt | sed -n ‘/mydoc1/,/mydoc41/p‘ | xargs -i cat {}> >mynew.txt
发现用其命令达不到预期效果,后来经检查发现,问题应该出现在sed排序上面,它仍然是按照位数来进行排序,并没有智能的按照范围来排序,sed取值命令如下:
代码如下:
ls-lF *.txt | sed-n ‘/mydoc1/,/mydoc41/p‘
命令显示结果如下所示:
代码如下:
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc10.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc11.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc12.txt
-rw-r--r-- 1 root root 3 Oct 12 14:48 mydoc13.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc14.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc15.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc16.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc17.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc18.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc19.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc1.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc20.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc21.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc22.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc23.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc24.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc25.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc26.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc27.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc28.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc29.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc2.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc30.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc31.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc32.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc33.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc34.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc35.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc36.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc37.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc38.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc39.txt
-rw-r--r-- 1 root root 2 Oct 12 14:48 mydoc3.txt
-rw-r--r-- 1 root root 0 Oct 12 13:38 mydoc40.txt
-rw-r--r-- 1 root root 6 Oct 12 14:48 mydoc41.txt
所以用如下命令来实现需求:命令如下所示:
代码如下:
for ((i=1;i<=41;i++))do echo mydoc$i.txt;done | xargs -i cat {} >> mynew.txt
这篇文章主要介绍了Shell中使用scp命令实现文件上传代码,本文讲解使用scp命令实现名登录上传文件,需要的朋友可以参考下
自从法现 SSH 可以执行远端命令后,ftp 软件也渐渐很少用了,
每次更新服务器代码,都要打包、上传、登录到服务器修改文件权限、更新 Cache 等等,又慢又容易出错,所以做成脚本,一次帮我搞定,自己就点上一根烟坐在电脑面前发呆,要么就玩会儿《植物大战僵尸》啥的,悠闲!
PS: Ant 以及 Phing 用户,你们可以无视了… 我只是喜欢玩 Shell 而已。
代码如下:
#!/bin/sh
HOME=‘/cygdrive/d/public_html/myproject‘
## 先打包本地代码
## 排除如下文件: *.svn, *.bat, upload.sh, cache
## 如果需要 exclude 的文件很多,可以使用参数 --exclude-from=FILE
## 将文件一行行写入 FILE 中
cd $HOME
tar jcf tmp.tar.bz2 *
--exclude=*.bat
--exclude=*.bz2
--exclude=*.gz
--exclude=.svn
--exclude=cache
--exclude=upload.sh
## 通过 SCP 上传文件
scp tmp.tar.bz2 user@your.host.com:/home/public_html/myproject/tmp.tar.bz2
## 执行远程 ssh 命令
## 这里执行了另外一个脚本 load.sh
ssh user@your.host.com
“
cd /home/public_html/myproject
tar jxf tmp.tar.bz2
chown -R web:web *
chmod -R 755 *
sh load.sh
rm -f tmp.tar.bz2
”
## 删除本地文件
rm -f tmp.tar.bz2
echo “Everything is done.”
# 星期一 一月 11, by Verdana
# vim: set expandtab tabstop=4 shiftwidth=4:
SSH 我配置成了自动登录,可以参考这里,
这篇文章主要介绍了Shell脚本实现从文件夹中递归复制文件,本文脚本实现从十层左右的文件夹中复制所有文件到一目录中,需要的朋友可以参考下
需求
前两天碰到需要在十层左右的文件夹中提取文件的需求,于是写了此脚本,
如下面这样的文件结构:
代码如下:
dir1
├── a
│ ├── b
│ │ └── file1
│ └── file2
├── c
│ └── d
│ ├── e
│ │ └── file4
│ └── file3
└── file5
我们需要将其中的file1~file5提取出来放到另一个文件夹中。
脚本
脚本getfilefromdir.sh如下:
代码如下:
#!/bin/bash
#desc: get file from directory
#example: sh getfilefromdir.sh A B
INIT_PATH=${1%/}
SAVE_PATH=${2%/}
function checksavepath() {
if [ -d $SAVE_PATH ]
then
rm -rf $SAVE_PATH
fi
mkdir ${SAVE_PATH}
touch $SAVE_PATH“.log”
}
function getfilefromdir(){
for file in ` ls $1`
do
if [ -d $1“/”$file ]
then
getfilefromdir $1“/”$file
else
local path=“$1/$file”
local name=$file
if [ ! -f $SAVE_PATH“/”$name ]
then
echo “cp ${path} to ${SAVE_PATH}/${name}”
cp ${path} “${SAVE_PATH}/${name}”
else
echo “${path} file already exists”
echo “${path}” >> $SAVE_PATH“.log” 2>&1
fi
fi
done
}
checksavepath
for sfol in ${INIT_PATH}
do
getfilefromdir ${sfol}
done
运行
代码如下:
sh getfilefromdir.sh dir1/ dir2
第一个参数是源文件夹,第二个是目地文件夹(不需要提前创建),
如果有同名文件,会存在dir2.log中
结果为:
代码如下:
dir2
├── file1
├── file2
├── file3
├── file4
└── file5