Linux CP using AWK output

I tried to learn more about Linux and this morning focused on the awk team. the command I tried to execute is below.

ls -lRt lpftp.* | awk '{print $7, $9}' | mkdir -p $(awk '{print $1}') | ls -lRt lpftp.* | cp $(awk '{print $9, $7}')

Essentially, I'm trying to move each file to a directory in a subdirectory based on the files of the last modified day. The command first prints only the files that I want, and then uses mkdir to create a folder depending on the day of the last change of the last month. What I want to do after this is move each file to its associated directory, however, since the command now moves each file to folder 01 and displays the following text

cp: 0653-436  12 is a directory.
    Specify -r or -R to copy.

once for each directory.

Does anyone know how I can fix this problem? or if there is a better way to do this?

+4
source share
2 answers

ls -lRt lpftp.* | awk '{print $7, $9}' | while read day file ; do mkdir -p "$day"; cp "$file" "$day"; done

Commands between doand donewill be executed for each line of output, with the first thing awkprinting in a variable day, and the second in file(in a line). I used the quotes here somewhat unnecessarily, since the variables should not have spaces specified by the method of setting them.

+3
source

- - - awk script. awk mkdir cp, . head (1), . , (1). :

ls -lRg lpfpt.* | awk script.awk | sh -ex

. , , x.

awk ( system):

  • , ,
+1

Source: https://habr.com/ru/post/1660714/


All Articles