Programing

유닉스-폴더 및 파일 경로 생성

lottogame 2020. 7. 18. 10:28
반응형

유닉스-폴더 및 파일 경로 생성


나는 당신이 할 수있는 알고있는 mkdir디렉토리를 생성하고 touch파일을 만들 수 있지만, 한 번에 두 작업을 할 수있는 방법이 없다?

즉, 폴더 other가 없을 때 아래 작업을 수행하려는 경우 :

cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt

오류:

cp: cannot create regular file `/my/other/path/here/cpedthing.txt': No such file or directory

누구든지 이것에 대한 해결 방법으로 기능을 생각해 냈습니까?


&&하나의 쉘 라인에서 두 명령을 결합하는 데 사용하십시오 .

COMMAND1 && COMMAND2
mkdir -p /my/other/path/here/ && touch /my/other/path/here/cpedthing.txt

참고 : 이전 ;에는 두 명령을 분리하는 방법을 권장 했지만 @trysis가 지적한 것처럼 실패 가 실행되지 &&않기 때문에 대부분의 상황에서 사용하는 것이 좋습니다 . 그렇지 않으면 예상하지 못한 문제가 발생할 수 있습니다.COMMAND1COMMAND2


모든 상위 디렉토리를 먼저 만들어야합니다.

FILE=./base/data/sounds/effects/camera_click.ogg

mkdir -p "$(dirname "$FILE")" && touch "$FILE"

창의력을 발휘하려면 다음과 같은 기능을 수행하십시오 .

mktouch() {
    if [ $# -lt 1 ]; then
        echo "Missing argument";
        return 1;
    fi

    for f in "$@"; do
        mkdir -p -- "$(dirname -- "$f")"
        touch -- "$f"
    done
}

그런 다음 다른 명령처럼 사용하십시오.

mktouch ./base/data/sounds/effects/camera_click.ogg ./some/other/file

/ usr / bin / install을 사용하여 수행하십시오.

install -D /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt

소스 파일이 없을 때 :

install -D <(echo 1) /my/other/path/here/cpedthing.txt

두 단계로 수행 할 수 있습니다.

mkdir -p /my/other/path/here/
touch /my/other/path/here/cpedthing.txt

#!/bin/sh
for f in "$@"; do mkdir -p "$(dirname "$f")"; done
touch "$@"

이것이 내가 할 일입니다.

mkdir -p /my/other/path/here && touch $_/cpredthing.txt

여기에서는 $_행에서 실행 한 이전 명령에 대한 마지막 인수를 나타내는 변수입니다.

항상 출력 결과를 확인하려면 다음 echo과 같이 명령 을 사용하여 테스트 할 수 있습니다 .

echo mkdir -p /code/temp/other/path/here && echo touch $_/cpredthing.txt

다음과 같은 출력 :

mkdir -p /code/temp/other/path/here
touch /code/temp/other/path/here/cpredthing.txt

보너스로, 중괄호 확장을 사용하여 한 번에 여러 파일을 작성할 수 있습니다. 예를 들면 다음과 같습니다.

mkdir -p /code/temp/other/path/here &&
touch $_/{cpredthing.txt,anotherfile,somescript.sh}

다시 한번, 다음과 echo같이 완전히 테스트 가능합니다 .

mkdir -p /code/temp/other/path/here
touch /code/temp/other/path/here/cpredthing.txt /code/temp/other/path/here/anotherfile /code/temp/other/path/here/somescript.sh

if [ ! -d /my/other ]
then
   mkdir /my/other/path/here
   cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
fi

유닉스 포럼에서보고 테스트 한 결과 문제가 해결되었습니다.

ptouch() {
    for p in "$@"; do
        _dir="$(dirname -- "$p")"
        [ -d "$_dir" ] || mkdir -p -- "$_dir"
    touch -- "$p"
    done
}

if then진술이 필요하지 않습니다 ... 당신은 한 줄 usign에서 그것을 할 수 있습니다;

mkdir -p /my/other/path/here;cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt

-또는 두 줄로-

mkdir -p /my/other/path/here
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt

-- the -p prevents error returns if the directory already exists (which is what I came here looking for :))


In the special (but not uncommon) case where you are trying to recreate the same directory hierarchy, cp --parents can be useful.

For example if /my/long contains the source files, and my/other already exists, you can do this:

cd /my/long
cp --parents path/here/thing.txt /my/other

if you want simple with only 1 param snippet :

rm -rf /abs/path/to/file;  #prevent cases when old file was a folder
mkdir -p /abs/path/to/file; #make it fist as a dir
rm -rf /abs/path/to/file; #remove the leaf of the dir preserving parents 
touch /abs/path/to/file; #create the actual file

참고URL : https://stackoverflow.com/questions/9452935/unix-create-path-of-folders-and-file

반응형