复制当前目录下多个目录的内容到新目录
Copy contents of multiple directories inside current directory into new directory
对 .sh
是全新的,我正在尝试从旧的 iPhone 备份图像,其中图像被放置到新目录中以供日期使用。有数百个目录,我需要从每个目录中获取图片并将它们全部转储到一个目录中。
我最好的尝试:
#!/bin/bash
function process () {
a=1
for i in *
do
cp -r i ${dir}
let a=a+1
done
}
#Interview
echo "This script will take the contents of each directory in the current directory and move it's contents into a new directory that you will specify"
echo "Name your new directory"
read dir
#Confirm
read -p "Your new directory will be ${dir}. Continue?" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
process
fi
收到错误:
massmove.sh: line 1: $'\r': command not found
massmove.sh: line 2: $'\r': command not found
massmove.sh: line 3: $'\r': command not found
massmove.sh: line 4: syntax error near unexpected token `$'{\r''
'assmove.sh: line 4: `function process () {
更新:通过 deefff 的回答改进:
function process () {
for i in */*.*
do
cp -r $i ${dir}
done
}
echo "This script will take the contents of each directory in the current directory and mv it's contents into a new directory that you will specify"
echo "Name your new directory"
read dir
mkdir ${dir}
echo process
仍然抛出这些错误:
massmove.sh: line 2: syntax error near unexpected token `$'{\r''
'assmove.sh: line 2: `function process () {
这可能是 WSL 错误吗?
我明白
cp */*.* ${dir}
是完成我的任务的一种快速而强大的方法,但我也对导致错误的原因非常感兴趣。
*
将匹配每个条目,包括目录,因此在当前形式下,它将复制整个目录结构,我想这不是您想要的。此外,您应该在 cp
命令中将变量 i
称为 $i
。此外, a
变量似乎毫无意义。像这样尝试:
function process () {
for i in */*.*
do
cp $i ${dir}
done
}
事实上,我刚刚意识到这也能解决问题:
cp */*.* ${dir}
尝试这样的事情:
function process () {
for file in *; do
if [[ ${file} != ${dir} ]]; then
cp -r ${file} ${dir}
fi
done
}
此外,记得在调用 'process' 函数之前创建目录:
echo "Name your new directory: "
read dir
mkdir ${dir}
请看下面我的脚本。
它基本上有3个功能:菜单、prnt(用于打印带日期的行)和process(处理文件)。它有一个菜单,可以与选项一起使用。希望大家喜欢
干杯!!
高拉夫
#!/bin/bash
curdir=`pwd` # Take the current directory
# To print out logs on the screen, with date and time !!!
prnt ()
{
d=`date +"%d-%m-%y "%T` # Take the date string using bash date function.
echo "$d|" # Whenever prnt is called, it will echo back, date plus the line passed to it, in nice date|line format.
}
# Menu for the whole operation. This is just the definition. Its being called from the bottom of the script.
menu()
{
# Specially made for you my friend !!
echo ; echo ; prnt " <<!!*** .. params_noob's file copier .. ***!!>>" ; echo ; echo
echo ; prnt "Currently we are in $curdir" ; echo
prnt "Enter the directory, where you would like to move files (Please enter full path and system will create it): " ; echo
read dir ; mkdir $dir 2>/dev/null # Read directory and make it. I am putting all errors to /dev/null. If directory is there, its there, don't want blabber. If not, then create it.
echo ;prnt "Entered directory is \"$dir\"" ; echo
prnt "Type y to go ahead OR n to start over again.." ; echo
read ans # I have read the answer here
# A simple menu using case function of bash.
case $ans in
y)
echo ; prnt "You have entered yes, to move ahead" ; echo
prnt "processing files now...."
process $dir # Here i am calling the process function.
;;
n)
echo ; prnt "Starting over.. " ; echo
menu # Here i am calling the menu itself, again.
;;
0)
echo ; prnt "Exiting now.. " ; echo
exit # Now exiting the script.
;;
*)
# This one is just to let you know, if you enter anything else than y,n or 0.
echo ; prnt "Invalid input. Please enter y or n or 0 !!" ; echo
;;
esac # Menu using case function ends with esac here.
}
# Function to process and move the files to the desired location.
process()
{
# Took the argument passed to this function into variable "td" [target dirctory].
td=""
# Using find command here to find all files. You an replace '-type f' with '-name "*.pic"' or '-name "*.jpg"' -- you get the picture - right?
find $curdir -type f | while read file # Feeding output of find to a while loop.
do
cp $file $td/
done
a=`ls -lrt $td/* |wc -l` # Now taking word count of all the files copied.
# Some more echo and priting.. just for aesthetics.. :)
echo ; echo ; prnt " **** COPIED $a FILES from \[$curdir\] to \[$td\] ****" ; echo ; echo
echo ; echo ; ls -lrtha $td|head -10 ; echo ; echo
exit # Script exits after processing the files. You can replace exit with menu, to go back to menu again.
}
#
##
###
####
#####
#################################################################
clear ; menu ### The main menu is being called from this line. I clear the screen first. Looks more professional :)
#################################################################
# FINISH # All done !!
enter code here
对 .sh
是全新的,我正在尝试从旧的 iPhone 备份图像,其中图像被放置到新目录中以供日期使用。有数百个目录,我需要从每个目录中获取图片并将它们全部转储到一个目录中。
我最好的尝试:
#!/bin/bash
function process () {
a=1
for i in *
do
cp -r i ${dir}
let a=a+1
done
}
#Interview
echo "This script will take the contents of each directory in the current directory and move it's contents into a new directory that you will specify"
echo "Name your new directory"
read dir
#Confirm
read -p "Your new directory will be ${dir}. Continue?" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
process
fi
收到错误:
massmove.sh: line 1: $'\r': command not found
massmove.sh: line 2: $'\r': command not found
massmove.sh: line 3: $'\r': command not found
massmove.sh: line 4: syntax error near unexpected token `$'{\r''
'assmove.sh: line 4: `function process () {
更新:通过 deefff 的回答改进:
function process () {
for i in */*.*
do
cp -r $i ${dir}
done
}
echo "This script will take the contents of each directory in the current directory and mv it's contents into a new directory that you will specify"
echo "Name your new directory"
read dir
mkdir ${dir}
echo process
仍然抛出这些错误:
massmove.sh: line 2: syntax error near unexpected token `$'{\r''
'assmove.sh: line 2: `function process () {
这可能是 WSL 错误吗?
我明白
cp */*.* ${dir}
是完成我的任务的一种快速而强大的方法,但我也对导致错误的原因非常感兴趣。
*
将匹配每个条目,包括目录,因此在当前形式下,它将复制整个目录结构,我想这不是您想要的。此外,您应该在 cp
命令中将变量 i
称为 $i
。此外, a
变量似乎毫无意义。像这样尝试:
function process () {
for i in */*.*
do
cp $i ${dir}
done
}
事实上,我刚刚意识到这也能解决问题:
cp */*.* ${dir}
尝试这样的事情:
function process () {
for file in *; do
if [[ ${file} != ${dir} ]]; then
cp -r ${file} ${dir}
fi
done
}
此外,记得在调用 'process' 函数之前创建目录:
echo "Name your new directory: "
read dir
mkdir ${dir}
请看下面我的脚本。
它基本上有3个功能:菜单、prnt(用于打印带日期的行)和process(处理文件)。它有一个菜单,可以与选项一起使用。希望大家喜欢
干杯!!
高拉夫
#!/bin/bash
curdir=`pwd` # Take the current directory
# To print out logs on the screen, with date and time !!!
prnt ()
{
d=`date +"%d-%m-%y "%T` # Take the date string using bash date function.
echo "$d|" # Whenever prnt is called, it will echo back, date plus the line passed to it, in nice date|line format.
}
# Menu for the whole operation. This is just the definition. Its being called from the bottom of the script.
menu()
{
# Specially made for you my friend !!
echo ; echo ; prnt " <<!!*** .. params_noob's file copier .. ***!!>>" ; echo ; echo
echo ; prnt "Currently we are in $curdir" ; echo
prnt "Enter the directory, where you would like to move files (Please enter full path and system will create it): " ; echo
read dir ; mkdir $dir 2>/dev/null # Read directory and make it. I am putting all errors to /dev/null. If directory is there, its there, don't want blabber. If not, then create it.
echo ;prnt "Entered directory is \"$dir\"" ; echo
prnt "Type y to go ahead OR n to start over again.." ; echo
read ans # I have read the answer here
# A simple menu using case function of bash.
case $ans in
y)
echo ; prnt "You have entered yes, to move ahead" ; echo
prnt "processing files now...."
process $dir # Here i am calling the process function.
;;
n)
echo ; prnt "Starting over.. " ; echo
menu # Here i am calling the menu itself, again.
;;
0)
echo ; prnt "Exiting now.. " ; echo
exit # Now exiting the script.
;;
*)
# This one is just to let you know, if you enter anything else than y,n or 0.
echo ; prnt "Invalid input. Please enter y or n or 0 !!" ; echo
;;
esac # Menu using case function ends with esac here.
}
# Function to process and move the files to the desired location.
process()
{
# Took the argument passed to this function into variable "td" [target dirctory].
td=""
# Using find command here to find all files. You an replace '-type f' with '-name "*.pic"' or '-name "*.jpg"' -- you get the picture - right?
find $curdir -type f | while read file # Feeding output of find to a while loop.
do
cp $file $td/
done
a=`ls -lrt $td/* |wc -l` # Now taking word count of all the files copied.
# Some more echo and priting.. just for aesthetics.. :)
echo ; echo ; prnt " **** COPIED $a FILES from \[$curdir\] to \[$td\] ****" ; echo ; echo
echo ; echo ; ls -lrtha $td|head -10 ; echo ; echo
exit # Script exits after processing the files. You can replace exit with menu, to go back to menu again.
}
#
##
###
####
#####
#################################################################
clear ; menu ### The main menu is being called from this line. I clear the screen first. Looks more professional :)
#################################################################
# FINISH # All done !!
enter code here