공백이 포함된 디렉토리경로내에 있는 특정내용이 담긴 파일을 삭제하고싶은데..

KJLEE@Naver의 이미지

안녕하세요

어떤내용이 담긴 파일을 삭제하고자 하는데 명령어를 잘 모르겠어서 질문드립니다

1234 내용이 담긴 test.txt 파일을 찾아서 삭제하고싶은데..
해당 파일의 경로에 공백이 포함된 디렉토리가 있는 경우
어떻게 삭제해야될지 모르겠습니다

저런게 한 몇십개 되서요...

find ./ -type f -name 'test.txt' -size 100c | xargs grep -l '1234' | xargs rm 를 입력하였을때
공백이 포함된 디렉토리는 경로는 검색이 되는데 '그런 파일이나 디렉토리가 없습니다' 라는
문구가 나오면서 삭제가 되지 않습니다

find 옵션에 -print0 를 grep쪽에도 -o를 넣어봤지만.. 증상이 똑같습니다

1234라는 내용은 예시입니다.

위 조건이 통하는 명령어를 알려주시면 감사하겠습니다.

세벌의 이미지

공백이 포함된 디렉토리는 경로는 검색이 되는데 '그런 파일이나 디렉토리가 없습니다' 라는
검색은 되는데, 파일이나 디렉토리가 없다? 무슨 말씀이신지 좀 더 자세히 설명 부탁드립니다.
KJLEE@Naver의 이미지

위에서 설명드린 명령어인 find와 grep을 이용하여
find ./* -type f -name 'index.html' | xargs grep -l '내용' 명령어를 입력했을때
---------------------------------------------
grep: ./000/: 디렉터리입니다
grep: photomoa_beautfy/index.html: 그런 파일이나 디렉터리가 없습니다
---------------------------------------------
라는 문구가 나옵니다.
(공백이 포함된 디렉토리의 경우 공백에서 아래로 분리됩니다.)

하지만 실제로 ./000/\ photomoa_beautfy/ 디렉토리로 들어가면 index.html 파일은 존재합니다.

find ./* -type f -name 'index.html' | xargs grep -l '내용' | xargs rm 명령어를 입력했을때에도
마찬가지로 위와 같은 문장이 나오며 삭제가 안됩니다..

bushi의 이미지

공백과는 상관없는 근본적인 문제가 있습니다.

$ echo "grep:/some where/something.txt: blah 1234 blah" | xargs rm
rm: cannot remove 'grep:/some': 그런 파일이나 디렉터리가 없습니다
rm: cannot remove 'where/something.txt:': 그런 파일이나 디렉터리가 없습니다
rm: cannot remove 'blah': 그런 파일이나 디렉터리가 없습니다
rm: cannot remove '1234': 그런 파일이나 디렉터리가 없습니다
rm: cannot remove 'blah': 그런 파일이나 디렉터리가 없습니다
 
$ echo "grep:/some where/something.txt: blah 1234" | (IFS=: read x y z; rm "$y")
rm: cannot remove '/some where/something.txt': 그런 파일이나 디렉터리가 없습니다
KJLEE@Naver의 이미지

근본적인 문제가 있다고 하시면..
제가 사용한 명령어로는 공백이 포함된 디렉토리가 있는 경로는 작업하기가 어려운건가요?

bushi의 이미지

xargs 로 입력되는 문자열 중 앞쪽의 "grep:" 을 없앨 수 있다면, 공백은 | xargs -ti rm {} 으로 처리할 수 있습니다.
그리고, find ./* 이 아니라 find ./ 혹은 find . 로 실행하세요.

KJLEE@Naver의 이미지

도와주셔서 감사합니다
xargs 명령어를 이용하여 해결한건 아니지만

find ./ -type f -name test.txt -size 100c -exec grep -l '내용' {} \; -exec rm -rf {} \;

명령어로 해결하였습니다

공백이 포함된 디렉터리명 하위에 있는 파일들중 특정 내용이 담긴 파일들을 한번에 처리하고 싶었는데
-exec 명령어를 두번쓸 수있다는것을 이제야알았습니다..

세벌의 이미지

find ./ -type f -name test.txt -size 100c -exec grep -l '내용' {} \; -exec rm -rf {} \;

수직막대(|)와 소문자 엘(l) 구별이 잘 안 되어서 코드 태그 안에 써 보았습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.