linux command to empty all files of a directory [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I´d like to empty all files from a directory. I´d tried this:
find myFolderPath/* -exec cat /dev/null > {} ';'
but it does not work. How can I do it?

You can't use redirection (>) within find -exec directly because it happens before the command runs and creates a file called {}. To get around this you need to do it in a new shell by using sh -c.
Also, note that you don't need to cat /dev/null > file in order to clobber a file. You can simply use > file.
Try this:
find . -type f -exec sh -c '>"{}"' \;

This will do what you want:
for f in *; do >$f; done

Related

"Find" command: highlight matching literal parts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I use find a lot, generally using the -name or -iname parameters.
I would like it to highlight the matching part in the files it finds (like grep does).
For example: find . -iname "*FOO*" would highlight instances for FOO
I know I could pipe it into grep but I'd rather not write two commands each time.
Is there a simple way to do it?
eg. like this:
find /home/ -type f | grep -i --color=always *.cpp

Linux is it possible to empty the contents of all files in a directory [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Is it possible in a single command (no loop) to clear the contents of each file in a directory?
Use truncate:
truncate -s 0 directory/* &> /dev/null
This is ugly as hell, but it works:
find . -type f -exec sh -c 'echo -n "" > $1' sh {} \;
This will clear every file in every subdirectory.
To just clear the files in the current directory:
for i in *; do cat /dev/null > $i; done
(Yes, it's a loop, but it's one line.)

Change filenames to lowercase in Ubuntu in all subdirectories [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I know it's been asked but what I've found has not worked out so far.
The closet I came is this : rename -n 'y[A-Z]/[a-z]/' *
which works for the current directory. I'm not too good at Linux terminal so what
should I add to this command to apply it to all of the files in all the sub-directories from which I am in, thanks!
Here's one way using find and tr:
for i in $(find . -type f -name "*[A-Z]*"); do mv "$i" "$(echo $i | tr A-Z a-z)"; done
Edit; added: -name "*[A-Z]*"
This ensures that only files with capital letters are found. For example, if files with only lowercase letters are found and moved to the same file, mv will display the are the same file error.
Perl has a locale-aware lc() function which might work better:
find . -type f | perl -n -e 'chomp; system("mv", $_, lc($_))'
Note that this script handles whitespace in filenames, but not newlines. And there's no protection against collisions, if you have "ASDF.txt" and "asdf.txt" one is going to get clobbered.

How can I use the `find` command in Linux to remove non-empty directories? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have temp directories full of junk that all start with __temp__ (e.g. __temp__user_uploads), which I want to delete with a cleanup function. My function attempt is to run:
find . -name __temp__* -exec rm -rf '{}' \;
If I run the command and there are multiple __temp__ directories (__temp__foo and __temp__bar), I get the output:
find: __temp__foo: unknown option
If I run the command and there is only 1 __temp__ directory (__temp__foo), it is deleted and I get the output:
find: ./__temp__foo: No such file or directory
Why doesn't the command work, why is it inconsistent like that, and how can I fix it?
Use a depth-first search and quote (or escape) the shell metacharacter *:
find . -depth -name '__temp__*' -exec rm -rf '{}' \;
Explanation
Without the -depth flag, your find command will remove matching filenames and then try to descend into the (now unlinked) directories. That's the origin of the "No such file or directory" in your single __temp__ directory case.
Without quoting or escaping the *, the shell will expand that pattern, matching several __temp__whatever filenames in the current working directory. This expansion will confuse find, which is expecting options rather than filenames at that point in its argument list.

How can I find all *.js file in directory recursively in Linux? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
In Linux, how can I find all *.js files in a directory recursively? The output should be an absolute path (like /pub/home/user1/folder/jses/file.js)
this answer worked for me:
find $PWD -name '*.js' > out.txt
It finds all *.js files, output absolute path, writes the results into out.txt.
find /abs/path/ -name '*.js'
Edit: As Brian points out, add -type f if you want only plain files, and not directories, links, etc.
Use find on the command line:
find /my/directory -name '*.js'
If you just want the list, then you should ask here: http://unix.stackexchange.com
The answer is: cd / && find -name *.js
If you want to implement this, you have to specify the language.

Resources