`
qtlkw
  • 浏览: 307124 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

UNIX: Loop Through Files In A Directory

 
阅读更多
for file in /path/to/file1.txt /path/to/file2.txt /path/to/file3.txt
do
# do something on $file
cat "$file"
done


You can directly process all command line args:


for file in $*
do
# do something on $file
[ -f "$file" ] && cat "$file"
done


OR simply process all *.css file in the current directory:


for file in *.css
do
# do something on "$file"
cat "$file" >> /var/www/cdn.example.com/cache/large.css
done


You can also read file names stored in a text file called delete.txt (note read with -r and IFS which will take care of file with spaces):


while IFS= read -r f <&3;
do
      #do something with "$f"
      rm -f "$f"
done 3< delete.txt


Make sure you always put $f or $file in double quote. Here is another sample script it will go through /home/wwwdata/{example.com,example.net,nixcraft.com} and process all files using for loop:

#!/bin/bash
# sync all domains to backup server at midnight
domains="example.com example.net nixcraft.com cyberciti.biz"
me="${0##*/}"
now=$(date +"%d-%m-%Y_%S")
log="/tmp/${me}.${now}"
latest="/tmp/latest"
logdata(){
local f="$1"
local d="$2"
[[ "$d" != "" ]] &&      echo "                            $d"
[[ "$f" == "start" ]] && echo "--------------------------------------------------------------"
[[ "$f" == "end" ]] &&   echo "=============================================================="

}
source /usr/local/nixcraft/mgmt/ssh/.keychain/$HOSTNAME-sh
for d in $domains
do
logdata "start" "$d @ $(date)"
        [ -d "/home/wwwdata/$d/" ] && { cd "/home/wwwdata/$d/";
/usr/bin/rsync  --exclude='cache/cache-*'\
--exclude '.bash_history' \
--exclude '.viminfo' \
--exclude 'cache/*_mutex.lock' \
--exclude 'broken-link-checker*' \
                        --exclude 'tmp/*'
-a --delete . backup@nasbox.nixcraft.net.in:/raid6/$HOSTNAME/ ;
         }
logdata "end" "$d @ $(date)"
done &> $log
[ -f $latest ] && /bin/rm -f $latest
ln -s $log $latest
mail -s "Backup $HOSTNAME" admin@clients.nixcraft.net.in < $latest

#!/bin/bash
export PATH
echo "$PATH"
rename .txt .xml *
for file in *.xml
do
 dos2unix "$file"
done
分享到:
评论

相关推荐

    UNIX, Third Edition: The Textbook 3rd Edition

    CHAPTER 2:A “Quick Start”into the UNIX Operating System CHAPTER 3:Editing Text Files CHAPTER 4:Files and File System Structure CHAPTER 5:File Security CHAPTER 6:Basic File Processing CHAPTER 7:...

    UNIX : A Hacking Tutorial

    **UNIX SYSTEM V** 是 UNIX 的一个重要版本,它是 AT&T 在20世纪80年代发布的,旨在统一 UNIX 的不同变体,并提供更先进的功能。本教程主要关注 SYSTEM V (版本3.2),这是一套非常成熟且广泛应用的 UNIX 版本。 ###...

    Win10 WSL运行docker报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.

    在Windows 10中使用Windows Subsystem for Linux (WSL) 运行Docker时,可能会遇到“Cannot connect to the Docker daemon at unix:///var/run/docker.sock.”的错误。这个错误通常意味着Docker守护进程(Docker ...

    Unix in a nutshell

    《Unix in a Nutshell》是一本专为Unix操作系统用户和开发者设计的综合指南,由Daniel Gilly和O'Reilly & Associates的团队合作编写。这本书详细介绍了Unix的各种命令、Shell、编辑器、脚本语言和工具,是理解Unix...

    nginx connect() to unix:/var/run/php-fpm.sock failed (11: Resource temporarily unavailable)

    connect() to unix:/var/run/php-fpm.sock failed (11: Resource temporarily unavailable) 发现phpfpm的listen是unix sock方式运行的,问题可能出在php fpm上。php fpm配置文件里有backlog,backlog是linux服务器在...

    gen_unix:Erlang Unix套接字接口

    gen_unix是Unix套接字的Erlang接口。 gen_unix的主要目的是允许访问fd和凭据传递所需的套接字辅助数据。 警告:gen_unix仍在开发中,并且需要进行很多更改。... 2&gt; {ok, Socket} = gen_unix:connect(Re

    报错:too many open files处理

    在IT行业中,我们经常遇到各种错误,其中之一是“too many open files”。这个错误通常发生在操作系统限制了同一时间可打开的文件数量,而程序试图超出这个限制时。在本篇文章中,我们将深入探讨这个问题,理解其...

    ubuntu vps安装docker报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.问题解决

    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 通过 service docker restart 尝试没有变化, 查看 docker 的日志 tail -5f /var/log/upstart/docker.log 发现...

    Linux下Socket编程的端口问题 ( Bind(): Address already in use ) PDF版

    ### Linux下Socket编程的端口问题 (Bind(): Address already in use) #### 一、问题背景与常见场景 在进行Linux下的网络编程时,经常会遇到端口绑定失败的问题,尤其是在使用`bind()`函数尝试绑定端口时,可能会...

    unix system programming in ocaml

    let src_fd = Unix.openfile src [Unix.O_RDONLY] 0o444 in let dst_fd = Unix.openfile dst [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o666 in let buf = Buffer.create 8192 in let rec copy_loop () = ...

    unix in a nutshell

    ### Unix in a Nutshell:Unix 入门经典书籍解析 #### 标题解析 - **标题**:“Unix in a Nutshell”(Unix 薄壳书) - 这个标题意味着本书是一本关于 Unix 的简介性指南,旨在提供一个快速、简洁的介绍。 - ...

    解决docker报错Cannot connect to the Docker daemon at unix

    解决docker报错Cannot connect to the Docker daemon at unix

    Unix In A Nutshell, 4th ed 2005

    Unix In A Nutshell, 4th ed 2005

    Unix shell programming in 24 hours.pdf

    - **Listing Files** The `ls` command is used to list the files and directories in a directory. Options like `-l` for detailed listings and `-a` for showing hidden files enhance its functionality. - **...

    unix笔记.pdf

    根据提供的文件信息,我们可以归纳出一系列关于UNIX操作系统的基础知识点,主要围绕登录与退出、目录操作命令及文件操作命令这三个方面进行展开。 ### 第一部分:登录和退出 #### 1. 登录 - 用户登录前需向系统...

    Systems Programming in Unix/Linux 1st Edition

    Systems Programming in Unix/Linux is intended as a textbook for systems programming courses in technically-oriented Computer Science/Engineering curricula that emphasize both theory and programming ...

    UNIX环境高级编程第二版.chm 英文版 Advanced Programming in the UNIX® Environment: Second Edition

    I just got my hands on a copy, and the first few chapters have been fascinating."--Open Systems Today"A much more readable and detailed treatment of UNIX internals can be found in Advanced ...

    UNIX in a Nutshell 3rd ed 1999

    Table of Contents Preface Part I: Commands and Shells Chapter 1: Introduction Chapter 2: Unix Commands Chapter 3: The Unix ...Appendix A: ASCII Character Set Appendix B: Obsolete Commands Bibliography

    英文原版-Unix in 24 Hours Sams Teach Yourself 5th Edition

    In just 24 lessons of one hour or less, Sams Teach Yourself Unix in 24 Hours helps you get up and running with Unix and Unix-based operating systems such as Mac OS X and Linux.Designed for beginners...

    Unix入门资料和Unix自学篇十讲

    UNIX 自学篇:第一讲:存取权限与文件 UNIX 自学篇:第二讲shell环境 UNIX 自学篇:第三讲vi editor UNIX 自学篇:第四讲:系统管理 UNIX 自学篇:第五讲:电子邮件 UNIX 自学篇:第六讲:stepl……step8…… UNIX ...

Global site tag (gtag.js) - Google Analytics