2017-11-07 3 views
0

나는 제대로 실행파일이있는 경우 파일을 이동하거나 복사 하시겠습니까?

mv /var/www/my_folder/reports.html /tmp/ 

명령을 실행하려합니다. 하지만 그 파일이 존재하고 그 명령 만 실행하는 것과 같은 조건을 넣고 싶습니다. 그런 것이 있습니까?

대신 쉘 파일을 넣을 수 있습니다. 쉘 는 일

if [ -e /var/www/my_folder/reports.html ] 
    then 
    mv /var/www/my_folder/reports.html /tmp/ 
fi 

아래 시도하지만 명령이 필요합니다. 어떤 사람이이 일을 도와 줄 수 있습니까?

+0

왜 정확하게 명령을 필요합니까 희망이 도움이 그것을 할 수 있습니까? –

답변

0

를 통해 이동하거나 에코 메시지 :

[ -f "/var/www/my_folder/reports.html" ] && mv "/var/www/my_folder/reports.html" /tmp/ 
  • -f - true 값을 반환을 파일이 있고 정규 파일 인 경우
+0

효과가 있습니다. 슈퍼 감사 :) –

+0

@ MaharjunM, 천만에요. – RomanPerekhrest

0

경우 파일이 존재하고 파일이 존재하는 경우에만 /var/www/my_folder/reports.html 및 일반 파일 이동 표준 오류 출력

test -e /var/www/my_folder/reports.html && mv /var/www/my_folder/reports.html /tmp/ || echo "not existing the file" >&2 
+0

또한 같은 것을 던지고 있습니다. –

+0

@RomanPerekhrest보다 질문에 대한 답변이 늦었습니다. 당신은 내 대답 시간을 볼 수 있습니다 : | – Daein

2

당신은 단순히 쉘 스크립트

#!/bin/bash 

# Check for the file 
ls /var/www/my_folder/ | grep reports.html > /dev/null 

# check output of the previous command 
if [ $? -eq 0 ] 
then 
    # echo -e "Found file" 
    mv /var/www/my_folder/reports.html /tmp/ 
else 
    # echo -e "File is not in there" 
fi 

그것이

관련 문제