Manipular vários arquivos por inode

2

Eu tenho um grande número de arquivos que contêm barras invertidas \ que eu gostaria de manipular, mas sempre que eu tento algo como:

$ ls -li
2036553851 -rw-rw-r-- 1 user user 6757 May 20 00:10 Simplex_config\B1:B3\_1.csv
2036553766 -rw-rw-r-- 1 user user 6756 May 20 00:07 Simplex_config\B1:B3\_2.csv
2036554099 -rw-rw-r-- 1 user user 6785 May 20 00:20 Simplex_config\B1:B3\_3.csv
2036553974 -rw-rw-r-- 1 user user 6785 May 20 00:15 Simplex_config\B1:B3\_4.csv

$ find . -type f -name 'Simplex*.csv' | xargs cat > looksee.txt

Eu recebo um erro No such file or directory . Eu considerei mudar os nomes dos arquivos e depois manipular, mas estou curioso para ver se havia uma solução mais fácil com o inode .

Eu inventei:

#!/bin/sh

if [ -f looksee.txt ]; then
   rm -rf looksee.txt
fi

ls -i Simplex_config*.csv | awk '{ print $1 }' > inode_list.txt

while IFS= read -r inode;
do
  find . -inum $inode -exec cat {} \; >> looksee.txt
done < inode_list.txt

Mas isso é muito complicado e eu gostaria de tentar encontrar uma maneira de analisar a saída de ls -i Simplex_config*.csv e canalizá-lo para outro comando em um one-liner - existe essa opção disponível?

    
por mlegge 20.05.2015 / 15:11

3 respostas

3

1.

find . -type f -name 'Simplex*.csv' -print0 | xargs -0 cat > looksee.txt

De man xargs

--null
-0
Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.

2.

find . -type f -name 'Simplex*.csv' -exec cat {} + > looksee.txt

De man find

-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ; is encountered. The string {} is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a \) or quoted to protect them from expansion by the shell. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

-exec command {} +
This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of {} is allowed within the command. The command is executed in the starting directory.

3.

cat Simplex_config* > looksee.txt

se você tiver apenas 1 nível de subpath.

    
por 20.05.2015 / 15:37
1

Você não pode acessar arquivos por inodes, porque isso quebraria o controle de acesso por meio de permissões. Por exemplo, se você não tiver permissão para percorrer um diretório, não poderá acessar nenhum dos arquivos desse diretório, independentemente das permissões do arquivo. Se você pudesse acessar um arquivo por inode, isso ignoraria as permissões de diretório.

Assim, enquanto você pode obter os números de dispositivo e inode de um arquivo, você precisa encontrar um caminho para o arquivo, a fim de agir sobre ele. (caminho A , não o caminho , já que pode haver mais de um se o arquivo tiver vários links físicos.) Isso significa que se você usar inodes, você sempre tem mais trabalho a fazer. A única razão para até mesmo olhar para inodes é se você quer estar ciente de hard links e agir apenas uma vez em cada arquivo com vários hard links.

Seu comando find é facilmente corrigido usando -print0 e xargs -0 (se disponível em seu sistema) ou usando a ação -exec . Para obter mais informações, consulte meu shell script engasga com espaço em branco ou outros caracteres especiais?

find . -type f -name 'Simplex*.csv' -exec cat {} + > looksee.txt
find . -type f -name 'Simplex*.csv' -print0 | xargs -0 cat > looksee.txt
    
por 21.05.2015 / 03:36
0

@Costas já lhe deu a melhor resposta, para reproduzir sua segunda pergunta

But this is very cumbersome and I would like to try to find a way to parse the output from ls -i Simplex_config*.csv and pipe it to another command in a one-liner -- is there such an option available?

você pode apenas usar xargs:

ls -i | cut -d ' ' -f 1 | xargs -I '$input' find -inum '$input' -exec cat {} \;

a opção -I permite que você especifique uma string para substituir pelo que lê a partir do stdin.

    
por 20.05.2015 / 15:40