如何连接文件列表

时间:2016-11-28 21:13:19

标签: bash unix terminal

我有一个交互式文件排序脚本,为它为排序输出创建的每个目录输出一系列filelist

如何将所有filelist的内容连接到桌面上的一个摘要文件。

除了这项特定任务外,我的代码效果很好。

#!/bin/bash
read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" all_extensions
if cd /Users/christopherdorman/desktop
  then  while read extension
      do    destination="folder$extension"
        mkdir -p "$destination"
        mv  -v unsorted/*."$extension" "$destination"
      done   <<< "${all_extensions// /$'\n'}"
        mkdir -p foldermisc 
        if mv  -v unsorted/* "foldermisc"
      then  echo "Good News, the rest of Your files have been successfully processed"
        fi
    for i in folder*/; do
        ls -S "$i" > "${i}filelist” 
        cat > "${i}filelist” | tee ~/desktop/summary.txt
    done
fi

2 个答案:

答案 0 :(得分:1)

此修改会截断summary.txt文件,然后将每个文件的cat放入所述摘要文件中。

#!/bin/bash
read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" all_extensions
if cd /Users/christopherdorman/desktop
  then  while read extension
      do    destination="folder$extension"
        mkdir -p "$destination"
        mv  -v unsorted/*."$extension" "$destination"
      done   <<< "${all_extensions// /$'\n'}"
        mkdir -p foldermisc 
        if mv  -v unsorted/* "foldermisc"
      then  echo "Good News, the rest of Your files have been successfully processed"
        fi
    truncate --size 0 ~/desktop/summary.txt
    for i in folder*/; do
        ls -S "$i" > "${i}filelist" 
        cat "${i}filelist" >> ~/desktop/summary.txt
    done
fi

答案 1 :(得分:0)

这两行中都有一个奇怪的字符(不是",相似但不相同):

    ls -S "$i" > "${i}filelist” 
    cat > "${i}filelist” | tee ~/desktop/summary.txt

将两者都改为这些(复制和粘贴):

    ls -S "$i" > "${i}filelist"
    cat "${i}filelist" | tee -a "summary.txt"

删除>将避免删除"${i}filelist” 添加-a会将append发球到现有文件。

或者,如果您愿意,只需使用以下行:

    ls -S "$i" | tee -a "${i}filelist" "summary.txt"

整个脚本应如下所示:

#!/bin/bash
mydesktop=/Users/christopherdorman/desktop
read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" all_extensions
if cd "$mydesktop"
then
    while  read extension
    do     destination="folder$extension"
           mkdir -p "$destination"
           mv -v unsorted/*."$extension" "$destination"
    done   <<< "${all_extensions// /$'\n'}"

    mkdir  -p foldermisc 

    if     mv  -v unsorted/* "foldermisc"
    then   echo "Good News, the rest of Your files have been successfully processed"
    fi

    for    i in folder*/
    do     ls -S "$i" | tee -a "${i}filelist" "summary.txt"
    done
fi