Using file partially for filenames

There are some commands that take file name and there are some case where you need to give file name. But there are some cases where you want to modify the file before passing it to the command. What do you do in these case’s?

I had a file containing huge amount of data and for some testing I wanted to pass only the first few lines of the file and not the complete file.  And since the file only accepted filename and would not take any input from the STDIN so only option was to create a file with the required data in a temporary file. So, I sat back to figure out some way to do it and finally found I can use this:

testcommand -f<(head -1 )

The “testcommand” parameter “-f” requires a filename. And here we are passing the file to the command after filtering it with head command. You can use any bash command inside the <() combination.

Enhanced by Zemanta

Linux find command – Find file and directories faster and easier

 

This command is very powerfull when used with combination of filters and pipes and RE.

I will give some example:

find . -type f -->> List all files
find . -type f -exec rm {} \\\; -->> Delete all files
find . -type d -exec rm {} \\; -->> Will through some common errors 🙂
find . -name \"*name*\"  --> find files containing name in the filename
find . -atime 12 --> Find files accessed 12 days ago

Similarly there is ctime for file status change

find . -type d -depth 2 --> find all the directories in the depth 2 of the tree.
find . -tyde d -ok rm -rf {} \\; --> Delete all the directories but only after user confirmation

There could be numerous such examples so I will leave it to you do digg the man page for find and then experiment :))

\"Reblog
Enhanced by Zemanta