Get the App
SLTechnology News&Howtos  ›  Development  › 

How to copy files to a specified directory in a recursive function in shell

Shulou Source: shulou.com Published: 2022-06-03 02:23:25 10月16日 Update

This article mainly introduces the shell recursive function how to circularly copy files to the designated directory, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Code:

#! / bin/shrecursive () {for file in $1 do subfile= `ls $2 $3 then rm $file` for subsubfile in $subfile do # echo $subsubfile if ["$subsubfile" = = "$2 $3/$file"] then rm-rf $3/$file mv $subsubfile $3/$file else recursive `ls $2 $3Compact $file` $2 $3/$file Fi done done} cd / tmpmkdir update;tar-zxvf update.tgz-C update;chmod-R 755 / tmp/update/*;rootdir= "/ tmp/update" filelist= `ls / tmp/update/ `for file in $filelistdo subfile= `ls $rootdir/$ file`for subsubfile in $subfile do if ["$subsubfile"! = "$rootdir/$file"] then $(recursive `ls $rootdir/$ file` $rootdir/$file) # rm-rf / $file/$subfile # mv $rootdir/$file/$subsubfile / $file break 1 Else kill-9 `ps | grep $file` rm-f / usr/bin/$file mv $rootdir/$file / usr/bin fi donedone#reboot

The following are new additions

Create soft links to all files in the folder, and linked files can be specified to be saved to other directories

When you assign a filelist, you can specify the source path

In the following sentence "ln-s $file arm-linux-" $lastwd ", you can specify the directory of the linked file, for example, the source path is the current path, and the destination path is also the current path.

# / bin/bash

Filelist= `ls. / `

For file in $filelist

Do

Firstwd=$file | cut-d "-"-F1

# if ["$firstwd" = = "arm"]

# then

Lastwd= `echo "$file" | sed's Unip. Sed-[] $/\ 1Unip'`

Echo $lastwd

Ln-s $file arm-linux- "$lastwd"

# fi

Done

Thank you for reading this article carefully. I hope the article "how to circularly copy files to a specified directory in shell" shared by the editor is helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: File path directory article link function copy recursion loop following text code value interest at the same time folder more friends goal knowledge tape Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Shulou Tech Info Docker Huawei